blob: b56d96f21f66583de998a7c3d3353fb24633ab54 [file] [log] [blame]
Chris Lattner226efd32010-11-23 19:19:34 +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 Lattner226efd32010-11-23 19:19:34 +000021#include "clang/Basic/FileSystemStatCache.h"
Chris Lattner2f4a89a2006-10-30 03:55:17 +000022#include "llvm/ADT/SmallString.h"
Chris Lattner84602242010-08-23 23:50:42 +000023#include "llvm/ADT/StringExtras.h"
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner3441b4f2009-08-23 22:45:33 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000026#include "llvm/System/Path.h"
Ted Kremenekd87eef82008-02-24 03:15:25 +000027#include "llvm/Config/config.h"
Benjamin Kramer26db6482009-09-05 09:49:39 +000028#include <map>
29#include <set>
30#include <string>
Chris Lattner22eb9722006-06-18 05:43:12 +000031using namespace clang;
32
33// FIXME: Enhance libsystem to support inode and other fields.
34#include <sys/stat.h>
35
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000036#if defined(_MSC_VER)
Chris Lattner882018b2009-02-12 01:37:35 +000037#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000038#endif
Chris Lattneraf653752006-10-30 03:06:54 +000039
Ted Kremenek8d71e252008-01-11 20:42:05 +000040/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000041/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000042#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000043
Chris Lattner9624b692010-11-23 20:50:22 +000044/// NON_EXISTENT_FILE - A special value distinct from null that is used to
45/// represent a filename that doesn't exist on the disk.
46#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
47
48
49FileEntry::~FileEntry() {
50 // If this FileEntry owns an open file descriptor that never got used, close
51 // it.
52 if (FD != -1) ::close(FD);
53}
54
Ted Kremenek5c04bd82009-01-28 00:27:31 +000055//===----------------------------------------------------------------------===//
56// Windows.
57//===----------------------------------------------------------------------===//
58
Ted Kremenekd87eef82008-02-24 03:15:25 +000059#ifdef LLVM_ON_WIN32
60
Benjamin Kramer3cf715d2010-11-21 11:32:22 +000061#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenekd87eef82008-02-24 03:15:25 +000062
63namespace {
Mike Stump11289f42009-09-09 15:08:12 +000064 static std::string GetFullPath(const char *relPath) {
Ted Kremenekd87eef82008-02-24 03:15:25 +000065 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
66 assert(absPathStrPtr && "_fullpath() returned NULL!");
67
68 std::string absPath(absPathStrPtr);
69
70 free(absPathStrPtr);
71 return absPath;
72 }
73}
74
75class FileManager::UniqueDirContainer {
76 /// UniqueDirs - Cache from full path to existing directories/files.
77 ///
Mike Stump11289f42009-09-09 15:08:12 +000078 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +000079
80public:
81 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
82 std::string FullPath(GetFullPath(Name));
Chris Lattner966b25b2010-11-23 20:30:42 +000083 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
Ted Kremenekd87eef82008-02-24 03:15:25 +000084 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner966b25b2010-11-23 20:30:42 +000086 size_t size() const { return UniqueDirs.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +000087};
88
89class FileManager::UniqueFileContainer {
90 /// UniqueFiles - Cache from full path to existing directories/files.
91 ///
Ted Kremenek1502b7e2009-01-28 01:01:07 +000092 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenekd87eef82008-02-24 03:15:25 +000093
94public:
95 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
96 std::string FullPath(GetFullPath(Name));
Chris Lattner84602242010-08-23 23:50:42 +000097
98 // LowercaseString because Windows filesystem is case insensitive.
99 FullPath = llvm::LowercaseString(FullPath);
Chris Lattner966b25b2010-11-23 20:30:42 +0000100 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
Ted Kremenekd87eef82008-02-24 03:15:25 +0000101 }
102
Chris Lattner966b25b2010-11-23 20:30:42 +0000103 size_t size() const { return UniqueFiles.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000104};
105
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000106//===----------------------------------------------------------------------===//
107// Unix-like Systems.
108//===----------------------------------------------------------------------===//
109
Ted Kremenekd87eef82008-02-24 03:15:25 +0000110#else
111
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000112#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenekd87eef82008-02-24 03:15:25 +0000113
114class FileManager::UniqueDirContainer {
115 /// UniqueDirs - Cache from ID's to existing directories/files.
Mike Stump11289f42009-09-09 15:08:12 +0000116 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000117
118public:
119 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
120 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
121 }
122
Chris Lattner966b25b2010-11-23 20:30:42 +0000123 size_t size() const { return UniqueDirs.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000124};
125
126class FileManager::UniqueFileContainer {
127 /// UniqueFiles - Cache from ID's to existing directories/files.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000128 std::set<FileEntry> UniqueFiles;
129
130public:
131 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
132 return
133 const_cast<FileEntry&>(
134 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000135 StatBuf.st_ino,
136 StatBuf.st_mode)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +0000137 }
138
Chris Lattner966b25b2010-11-23 20:30:42 +0000139 size_t size() const { return UniqueFiles.size(); }
Ted Kremenekd87eef82008-02-24 03:15:25 +0000140};
141
142#endif
143
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000144//===----------------------------------------------------------------------===//
145// Common logic.
146//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +0000147
Chris Lattner3f5a9ef2010-11-23 07:51:02 +0000148FileManager::FileManager(const FileSystemOptions &FSO)
149 : FileSystemOpts(FSO),
Chris Lattner966b25b2010-11-23 20:30:42 +0000150 UniqueDirs(*new UniqueDirContainer()),
151 UniqueFiles(*new UniqueFileContainer()),
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000152 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +0000153 NumDirLookups = NumFileLookups = 0;
154 NumDirCacheMisses = NumFileCacheMisses = 0;
155}
156
157FileManager::~FileManager() {
158 delete &UniqueDirs;
159 delete &UniqueFiles;
Chris Lattner966b25b2010-11-23 20:30:42 +0000160 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
161 delete VirtualFileEntries[i];
Ted Kremenekd87eef82008-02-24 03:15:25 +0000162}
163
Chris Lattner226efd32010-11-23 19:19:34 +0000164void FileManager::addStatCache(FileSystemStatCache *statCache,
165 bool AtBeginning) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000166 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
Chris Lattner226efd32010-11-23 19:19:34 +0000173 FileSystemStatCache *LastCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000174 while (LastCache->getNextStatCache())
175 LastCache = LastCache->getNextStatCache();
176
177 LastCache->setNextStatCache(statCache);
178}
179
Chris Lattner226efd32010-11-23 19:19:34 +0000180void FileManager::removeStatCache(FileSystemStatCache *statCache) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000181 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.
Chris Lattner226efd32010-11-23 19:19:34 +0000191 FileSystemStatCache *PrevCache = StatCache.get();
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000192 while (PrevCache && PrevCache->getNextStatCache() != statCache)
193 PrevCache = PrevCache->getNextStatCache();
Chris Lattner9624b692010-11-23 20:50:22 +0000194
195 assert(PrevCache && "Stat cache not found for removal");
196 PrevCache->setNextStatCache(statCache->getNextStatCache());
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000197}
198
Douglas Gregor407e2122009-12-02 18:12:28 +0000199/// \brief Retrieve the directory that the given file name resides in.
200static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
Chris Lattner5159f612010-11-23 08:35:12 +0000201 llvm::StringRef Filename) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000202 // Figure out what directory it is in. If the string contains a / in it,
203 // strip off everything after it.
204 // FIXME: this logic should be in sys::Path.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000205 size_t SlashPos = Filename.size();
206 while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
207 --SlashPos;
208
Chris Lattner0c0e8042010-11-21 09:50:16 +0000209 // Use the current directory if file has no path component.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000210 if (SlashPos == 0)
Chris Lattner5159f612010-11-23 08:35:12 +0000211 return FileMgr.getDirectory(".");
Douglas Gregor407e2122009-12-02 18:12:28 +0000212
Chris Lattner0c0e8042010-11-21 09:50:16 +0000213 if (SlashPos == Filename.size()-1)
Douglas Gregor407e2122009-12-02 18:12:28 +0000214 return 0; // If filename ends with a /, it's a directory.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000215
Chris Lattner0c0e8042010-11-21 09:50:16 +0000216 // Ignore repeated //'s.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000217 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattner0c0e8042010-11-21 09:50:16 +0000218 --SlashPos;
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000219
Chris Lattner5159f612010-11-23 08:35:12 +0000220 return FileMgr.getDirectory(Filename.substr(0, SlashPos));
Douglas Gregor407e2122009-12-02 18:12:28 +0000221}
222
Chris Lattner22eb9722006-06-18 05:43:12 +0000223/// getDirectory - Lookup, cache, and verify the specified directory. This
224/// returns null if the directory doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000225///
Chris Lattner5159f612010-11-23 08:35:12 +0000226const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename) {
John Thompson551446b2009-12-18 14:18:21 +0000227 // stat doesn't like trailing separators (at least on Windows).
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000228 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattner0c0e8042010-11-21 09:50:16 +0000229 Filename = Filename.substr(0, Filename.size()-1);
John Thompson551446b2009-12-18 14:18:21 +0000230
Chris Lattner22eb9722006-06-18 05:43:12 +0000231 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000232 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000233 DirEntries.GetOrCreateValue(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner22eb9722006-06-18 05:43:12 +0000235 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000236 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000237 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000238 ? 0 : NamedDirEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000239
Chris Lattner22eb9722006-06-18 05:43:12 +0000240 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattneraf653752006-10-30 03:06:54 +0000242 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000243 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattner43fd42e2006-10-30 03:40:58 +0000245 // Get the null-terminated directory name as stored as the key of the
246 // DirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000247 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000248
Chris Lattneraf653752006-10-30 03:06:54 +0000249 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +0000250 struct stat StatBuf;
Chris Lattner9624b692010-11-23 20:50:22 +0000251 if (getStatValue(InterndDirName, StatBuf, true))
Chris Lattner22eb9722006-06-18 05:43:12 +0000252 return 0;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000253
Chris Lattner22eb9722006-06-18 05:43:12 +0000254 // It exists. See if we have already opened a directory with the same inode.
Mike Stump11289f42009-09-09 15:08:12 +0000255 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000256 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000257
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000258 NamedDirEnt.setValue(&UDE);
259 if (UDE.getName()) // Already have an entry with this inode, return it.
260 return &UDE;
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattnera85cbe22006-10-30 03:11:40 +0000262 // Otherwise, we don't have this directory yet, add it. We use the string
263 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000264 UDE.Name = InterndDirName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000265 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000266}
267
268/// getFile - Lookup, cache, and verify the specified file. This returns null
269/// if the file doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000270///
Chris Lattner5159f612010-11-23 08:35:12 +0000271const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner22eb9722006-06-18 05:43:12 +0000274 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000275 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000276 FileEntries.GetOrCreateValue(Filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000277
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000278 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000279 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000280 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000281 ? 0 : NamedFileEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner22eb9722006-06-18 05:43:12 +0000283 ++NumFileCacheMisses;
284
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000285 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000286 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000287
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000289 // Get the null-terminated file name as stored as the key of the
290 // FileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000291 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattner966b25b2010-11-23 20:30:42 +0000293
294 // Look up the directory for the file. When looking up something like
295 // sys/foo.h we'll discover all of the search directories that have a 'sys'
296 // subdirectory. This will let us avoid having to waste time on known-to-fail
297 // searches when we go to find sys/bar.h, because all the search directories
298 // without a 'sys' subdir will get a cached failure result.
Chris Lattner5159f612010-11-23 08:35:12 +0000299 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000300 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
301 return 0;
302
Chris Lattner22eb9722006-06-18 05:43:12 +0000303 // FIXME: Use the directory info to prune this, before doing the stat syscall.
304 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chris Lattner22eb9722006-06-18 05:43:12 +0000306 // Nope, there isn't. Check to see if the file exists.
307 struct stat StatBuf;
Chris Lattner9624b692010-11-23 20:50:22 +0000308 if (getStatValue(InterndFileName, StatBuf, false))
Chris Lattner22eb9722006-06-18 05:43:12 +0000309 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000310
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000311 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000312 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000313 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000315 NamedFileEnt.setValue(&UFE);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000316 if (UFE.getName()) // Already have an entry with this inode, return it.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000317 return &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000318
Chris Lattner22eb9722006-06-18 05:43:12 +0000319 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000320 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
321 // key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000322 UFE.Name = InterndFileName;
323 UFE.Size = StatBuf.st_size;
324 UFE.ModTime = StatBuf.st_mtime;
325 UFE.Dir = DirInfo;
326 UFE.UID = NextFileUID++;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000327 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000328}
329
Douglas Gregor407e2122009-12-02 18:12:28 +0000330const FileEntry *
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000331FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Chris Lattner5159f612010-11-23 08:35:12 +0000332 time_t ModificationTime) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000333 ++NumFileLookups;
334
335 // See if there is already an entry in the map.
336 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000337 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000338
339 // See if there is already an entry in the map.
340 if (NamedFileEnt.getValue())
341 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
342 ? 0 : NamedFileEnt.getValue();
343
344 ++NumFileCacheMisses;
345
346 // By default, initialize it to invalid.
347 NamedFileEnt.setValue(NON_EXISTENT_FILE);
348
Chris Lattner5159f612010-11-23 08:35:12 +0000349 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000350 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
351 return 0;
352
353 FileEntry *UFE = new FileEntry();
354 VirtualFileEntries.push_back(UFE);
355 NamedFileEnt.setValue(UFE);
356
Chris Lattner9624b692010-11-23 20:50:22 +0000357 // Get the null-terminated file name as stored as the key of the
358 // FileEntries map.
359 const char *InterndFileName = NamedFileEnt.getKeyData();
360
361 UFE->Name = InterndFileName;
Douglas Gregor407e2122009-12-02 18:12:28 +0000362 UFE->Size = Size;
363 UFE->ModTime = ModificationTime;
364 UFE->Dir = DirInfo;
365 UFE->UID = NextFileUID++;
Douglas Gregor81c000f2010-07-26 23:54:23 +0000366
367 // If this virtual file resolves to a file, also map that file to the
368 // newly-created file entry.
Douglas Gregor81c000f2010-07-26 23:54:23 +0000369 struct stat StatBuf;
Chris Lattner9624b692010-11-23 20:50:22 +0000370 if (getStatValue(InterndFileName, StatBuf, false))
371 return UFE;
372
373 llvm::sys::Path FilePath(UFE->Name);
374 FilePath.makeAbsolute();
375 FileEntries[FilePath.str()] = UFE;
Douglas Gregor407e2122009-12-02 18:12:28 +0000376 return UFE;
377}
378
Chris Lattner6e640992010-11-23 04:45:28 +0000379void FileManager::FixupRelativePath(llvm::sys::Path &path,
380 const FileSystemOptions &FSOpts) {
381 if (FSOpts.WorkingDir.empty() || path.isAbsolute()) return;
382
383 llvm::sys::Path NewPath(FSOpts.WorkingDir);
384 NewPath.appendComponent(path.str());
385 path = NewPath;
386}
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000387
Chris Lattner6e640992010-11-23 04:45:28 +0000388llvm::MemoryBuffer *FileManager::
Chris Lattner26b5c192010-11-23 09:19:42 +0000389getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
390 llvm::StringRef Filename = Entry->getName();
Chris Lattner6e640992010-11-23 04:45:28 +0000391 if (FileSystemOpts.WorkingDir.empty())
Chris Lattnerb3c81452010-11-23 19:38:22 +0000392 return llvm::MemoryBuffer::getFile(Filename, ErrorStr, Entry->getSize());
Chris Lattner6e640992010-11-23 04:45:28 +0000393
394 llvm::sys::Path FilePath(Filename);
395 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattnerb3c81452010-11-23 19:38:22 +0000396 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr,
397 Entry->getSize());
Chris Lattner26b5c192010-11-23 09:19:42 +0000398}
399
400llvm::MemoryBuffer *FileManager::
401getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
402 if (FileSystemOpts.WorkingDir.empty())
403 return llvm::MemoryBuffer::getFile(Filename, ErrorStr);
404
405 llvm::sys::Path FilePath(Filename);
406 FixupRelativePath(FilePath, FileSystemOpts);
407 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000408}
409
Chris Lattner226efd32010-11-23 19:19:34 +0000410/// getStatValue - Get the 'stat' information for the specified path, using the
411/// cache to accellerate it if possible. This returns true if the path does not
412/// exist or false if it exists.
Chris Lattner9624b692010-11-23 20:50:22 +0000413///
414/// The isForDir member indicates whether this is a directory lookup or not.
415/// This will return failure if the lookup isn't the expected kind.
416bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
417 bool isForDir) {
Chris Lattner226efd32010-11-23 19:19:34 +0000418 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
419 // absolute!
Chris Lattner5769c3d2010-11-23 19:56:39 +0000420 if (FileSystemOpts.WorkingDir.empty())
Chris Lattner9624b692010-11-23 20:50:22 +0000421 return FileSystemStatCache::get(Path, StatBuf, StatCache.get()) ||
422 S_ISDIR(StatBuf.st_mode) != isForDir;
Chris Lattner226efd32010-11-23 19:19:34 +0000423
424 llvm::sys::Path FilePath(Path);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000425 FixupRelativePath(FilePath, FileSystemOpts);
Chris Lattner5769c3d2010-11-23 19:56:39 +0000426
Chris Lattner9624b692010-11-23 20:50:22 +0000427 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, StatCache.get()) ||
428 S_ISDIR(StatBuf.st_mode) != isForDir;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000429}
430
Chris Lattner226efd32010-11-23 19:19:34 +0000431
432
Chris Lattner22eb9722006-06-18 05:43:12 +0000433void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000434 llvm::errs() << "\n*** File Manager Stats:\n";
435 llvm::errs() << UniqueFiles.size() << " files found, "
436 << UniqueDirs.size() << " dirs found.\n";
437 llvm::errs() << NumDirLookups << " dir lookups, "
438 << NumDirCacheMisses << " dir cache misses.\n";
439 llvm::errs() << NumFileLookups << " file lookups, "
440 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000441
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000442 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000443}
Douglas Gregorc5046832009-04-27 18:38:38 +0000444