blob: ff07c79cdf8d81507d14c5453519031fcdb437cd [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"
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000021#include "clang/Basic/FileSystemOptions.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
Ted Kremenek5c04bd82009-01-28 00:27:31 +000044//===----------------------------------------------------------------------===//
45// Windows.
46//===----------------------------------------------------------------------===//
47
Ted Kremenekd87eef82008-02-24 03:15:25 +000048#ifdef LLVM_ON_WIN32
49
Benjamin Kramer3cf715d2010-11-21 11:32:22 +000050#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
Ted Kremenekd87eef82008-02-24 03:15:25 +000051
52namespace {
Mike Stump11289f42009-09-09 15:08:12 +000053 static std::string GetFullPath(const char *relPath) {
Ted Kremenekd87eef82008-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 Stump11289f42009-09-09 15:08:12 +000067 llvm::StringMap<DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-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 Stump11289f42009-09-09 15:08:12 +000077
Ted Kremenekd87eef82008-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 Kremenek1502b7e2009-01-28 01:01:07 +000084 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenekd87eef82008-02-24 03:15:25 +000085
86public:
87 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
88 std::string FullPath(GetFullPath(Name));
Chris Lattner84602242010-08-23 23:50:42 +000089
90 // LowercaseString because Windows filesystem is case insensitive.
91 FullPath = llvm::LowercaseString(FullPath);
Ted Kremenekd87eef82008-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 Kremenek5c04bd82009-01-28 00:27:31 +0000101//===----------------------------------------------------------------------===//
102// Unix-like Systems.
103//===----------------------------------------------------------------------===//
104
Ted Kremenekd87eef82008-02-24 03:15:25 +0000105#else
106
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000107#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
Ted Kremenekd87eef82008-02-24 03:15:25 +0000108
109class FileManager::UniqueDirContainer {
110 /// UniqueDirs - Cache from ID's to existing directories/files.
111 ///
Mike Stump11289f42009-09-09 15:08:12 +0000112 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
Ted Kremenekd87eef82008-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 Kremenek5d7e2e12009-02-12 03:17:57 +0000132 StatBuf.st_ino,
133 StatBuf.st_mode)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +0000134 }
135
136 size_t size() { return UniqueFiles.size(); }
137};
138
139#endif
140
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000141//===----------------------------------------------------------------------===//
142// Common logic.
143//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +0000144
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000145FileManager::FileManager()
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000146 : UniqueDirs(*new UniqueDirContainer),
147 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000148 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-02-24 03:15:25 +0000149 NumDirLookups = NumFileLookups = 0;
150 NumDirCacheMisses = NumFileCacheMisses = 0;
151}
152
153FileManager::~FileManager() {
154 delete &UniqueDirs;
155 delete &UniqueFiles;
Douglas Gregor407e2122009-12-02 18:12:28 +0000156 for (llvm::SmallVectorImpl<FileEntry *>::iterator
157 V = VirtualFileEntries.begin(),
158 VEnd = VirtualFileEntries.end();
159 V != VEnd;
160 ++V)
161 delete *V;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000162}
163
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000164void FileManager::addStatCache(StatSysCallCache *statCache, bool AtBeginning) {
165 assert(statCache && "No stat cache provided?");
166 if (AtBeginning || StatCache.get() == 0) {
167 statCache->setNextStatCache(StatCache.take());
168 StatCache.reset(statCache);
169 return;
170 }
171
172 StatSysCallCache *LastCache = StatCache.get();
173 while (LastCache->getNextStatCache())
174 LastCache = LastCache->getNextStatCache();
175
176 LastCache->setNextStatCache(statCache);
177}
178
179void FileManager::removeStatCache(StatSysCallCache *statCache) {
180 if (!statCache)
181 return;
182
183 if (StatCache.get() == statCache) {
184 // This is the first stat cache.
185 StatCache.reset(StatCache->takeNextStatCache());
186 return;
187 }
188
189 // Find the stat cache in the list.
190 StatSysCallCache *PrevCache = StatCache.get();
191 while (PrevCache && PrevCache->getNextStatCache() != statCache)
192 PrevCache = PrevCache->getNextStatCache();
193 if (PrevCache)
194 PrevCache->setNextStatCache(statCache->getNextStatCache());
195 else
196 assert(false && "Stat cache not found for removal");
197}
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 Lattner0c0e8042010-11-21 09:50:16 +0000201 llvm::StringRef Filename,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000202 const FileSystemOptions &FileSystemOpts) {
Douglas Gregor407e2122009-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 Kramer3cf715d2010-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 Lattner0c0e8042010-11-21 09:50:16 +0000210 // Use the current directory if file has no path component.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000211 if (SlashPos == 0)
Chris Lattner0c0e8042010-11-21 09:50:16 +0000212 return FileMgr.getDirectory(".", FileSystemOpts);
Douglas Gregor407e2122009-12-02 18:12:28 +0000213
Chris Lattner0c0e8042010-11-21 09:50:16 +0000214 if (SlashPos == Filename.size()-1)
Douglas Gregor407e2122009-12-02 18:12:28 +0000215 return 0; // If filename ends with a /, it's a directory.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000216
Chris Lattner0c0e8042010-11-21 09:50:16 +0000217 // Ignore repeated //'s.
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000218 while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
Chris Lattner0c0e8042010-11-21 09:50:16 +0000219 --SlashPos;
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000220
Chris Lattner0c0e8042010-11-21 09:50:16 +0000221 return FileMgr.getDirectory(Filename.substr(0, SlashPos), FileSystemOpts);
Douglas Gregor407e2122009-12-02 18:12:28 +0000222}
223
Chris Lattner22eb9722006-06-18 05:43:12 +0000224/// getDirectory - Lookup, cache, and verify the specified directory. This
225/// returns null if the directory doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000226///
Chris Lattner0c0e8042010-11-21 09:50:16 +0000227const DirectoryEntry *FileManager::getDirectory(llvm::StringRef Filename,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000228 const FileSystemOptions &FileSystemOpts) {
John Thompson551446b2009-12-18 14:18:21 +0000229 // stat doesn't like trailing separators (at least on Windows).
Benjamin Kramer3cf715d2010-11-21 11:32:22 +0000230 if (Filename.size() > 1 && IS_DIR_SEPARATOR_CHAR(Filename.back()))
Chris Lattner0c0e8042010-11-21 09:50:16 +0000231 Filename = Filename.substr(0, Filename.size()-1);
John Thompson551446b2009-12-18 14:18:21 +0000232
Chris Lattner22eb9722006-06-18 05:43:12 +0000233 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000234 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000235 DirEntries.GetOrCreateValue(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattner22eb9722006-06-18 05:43:12 +0000237 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000238 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000239 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000240 ? 0 : NamedDirEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattner22eb9722006-06-18 05:43:12 +0000242 ++NumDirCacheMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000243
Chris Lattneraf653752006-10-30 03:06:54 +0000244 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000245 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Mike Stump11289f42009-09-09 15:08:12 +0000246
Chris Lattner43fd42e2006-10-30 03:40:58 +0000247 // Get the null-terminated directory name as stored as the key of the
248 // DirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000249 const char *InterndDirName = NamedDirEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattneraf653752006-10-30 03:06:54 +0000251 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +0000252 struct stat StatBuf;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000253 if (stat_cached(InterndDirName, &StatBuf, FileSystemOpts) || // Error stat'ing.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000254 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Chris Lattner22eb9722006-06-18 05:43:12 +0000255 return 0;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000256
Chris Lattner22eb9722006-06-18 05:43:12 +0000257 // It exists. See if we have already opened a directory with the same inode.
Mike Stump11289f42009-09-09 15:08:12 +0000258 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000259 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000260
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000261 NamedDirEnt.setValue(&UDE);
262 if (UDE.getName()) // Already have an entry with this inode, return it.
263 return &UDE;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Chris Lattnera85cbe22006-10-30 03:11:40 +0000265 // Otherwise, we don't have this directory yet, add it. We use the string
266 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000267 UDE.Name = InterndDirName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000268 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000269}
270
Ted Kremenek8d71e252008-01-11 20:42:05 +0000271/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000272/// represent a filename that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000273#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000274
Chris Lattner22eb9722006-06-18 05:43:12 +0000275/// getFile - Lookup, cache, and verify the specified file. This returns null
276/// if the file doesn't exist.
Mike Stump11289f42009-09-09 15:08:12 +0000277///
Chris Lattner0c0e8042010-11-21 09:50:16 +0000278const FileEntry *FileManager::getFile(llvm::StringRef Filename,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000279 const FileSystemOptions &FileSystemOpts) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000280 ++NumFileLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000283 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000284 FileEntries.GetOrCreateValue(Filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000285
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000286 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000287 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000288 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000289 ? 0 : NamedFileEnt.getValue();
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner22eb9722006-06-18 05:43:12 +0000291 ++NumFileCacheMisses;
292
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000293 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000294 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000295
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000297 // Get the null-terminated file name as stored as the key of the
298 // FileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000299 const char *InterndFileName = NamedFileEnt.getKeyData();
Mike Stump11289f42009-09-09 15:08:12 +0000300
Douglas Gregor407e2122009-12-02 18:12:28 +0000301 const DirectoryEntry *DirInfo
Chris Lattner0c0e8042010-11-21 09:50:16 +0000302 = getDirectoryFromFile(*this, Filename, FileSystemOpts);
Douglas Gregor407e2122009-12-02 18:12:28 +0000303 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
304 return 0;
305
Chris Lattner22eb9722006-06-18 05:43:12 +0000306 // FIXME: Use the directory info to prune this, before doing the stat syscall.
307 // FIXME: This will reduce the # syscalls.
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner22eb9722006-06-18 05:43:12 +0000309 // Nope, there isn't. Check to see if the file exists.
310 struct stat StatBuf;
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000311 //llvm::errs() << "STATING: " << Filename;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000312 if (stat_cached(InterndFileName, &StatBuf, FileSystemOpts) || // Error stat'ing.
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000313 S_ISDIR(StatBuf.st_mode)) { // A directory?
Chris Lattner81500bc2006-07-19 03:40:07 +0000314 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000315 //llvm::errs() << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000316 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000317 }
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000318 //llvm::errs() << ": exists\n";
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000320 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000321 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000322 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Mike Stump11289f42009-09-09 15:08:12 +0000323
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000324 NamedFileEnt.setValue(&UFE);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000325 if (UFE.getName()) // Already have an entry with this inode, return it.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000326 return &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000327
Chris Lattner22eb9722006-06-18 05:43:12 +0000328 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000329 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
330 // key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000331 UFE.Name = InterndFileName;
332 UFE.Size = StatBuf.st_size;
333 UFE.ModTime = StatBuf.st_mtime;
334 UFE.Dir = DirInfo;
335 UFE.UID = NextFileUID++;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000336 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000337}
338
Douglas Gregor407e2122009-12-02 18:12:28 +0000339const FileEntry *
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000340FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000341 time_t ModificationTime,
342 const FileSystemOptions &FileSystemOpts) {
Douglas Gregor407e2122009-12-02 18:12:28 +0000343 ++NumFileLookups;
344
345 // See if there is already an entry in the map.
346 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner0c0e8042010-11-21 09:50:16 +0000347 FileEntries.GetOrCreateValue(Filename);
Douglas Gregor407e2122009-12-02 18:12:28 +0000348
349 // See if there is already an entry in the map.
350 if (NamedFileEnt.getValue())
351 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
352 ? 0 : NamedFileEnt.getValue();
353
354 ++NumFileCacheMisses;
355
356 // By default, initialize it to invalid.
357 NamedFileEnt.setValue(NON_EXISTENT_FILE);
358
359 const DirectoryEntry *DirInfo
Chris Lattner0c0e8042010-11-21 09:50:16 +0000360 = getDirectoryFromFile(*this, Filename, FileSystemOpts);
Douglas Gregor407e2122009-12-02 18:12:28 +0000361 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
362 return 0;
363
364 FileEntry *UFE = new FileEntry();
365 VirtualFileEntries.push_back(UFE);
366 NamedFileEnt.setValue(UFE);
367
368 UFE->Name = NamedFileEnt.getKeyData();
369 UFE->Size = Size;
370 UFE->ModTime = ModificationTime;
371 UFE->Dir = DirInfo;
372 UFE->UID = NextFileUID++;
Douglas Gregor81c000f2010-07-26 23:54:23 +0000373
374 // If this virtual file resolves to a file, also map that file to the
375 // newly-created file entry.
376 const char *InterndFileName = NamedFileEnt.getKeyData();
377 struct stat StatBuf;
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000378 if (!stat_cached(InterndFileName, &StatBuf, FileSystemOpts) &&
Douglas Gregor81c000f2010-07-26 23:54:23 +0000379 !S_ISDIR(StatBuf.st_mode)) {
380 llvm::sys::Path FilePath(InterndFileName);
381 FilePath.makeAbsolute();
382 FileEntries[FilePath.str()] = UFE;
383 }
384
Douglas Gregor407e2122009-12-02 18:12:28 +0000385 return UFE;
386}
387
Chris Lattner0c0e8042010-11-21 09:50:16 +0000388llvm::MemoryBuffer *FileManager::
389getBufferForFile(const char *FilenameStart, const char *FilenameEnd,
390 const FileSystemOptions &FileSystemOpts,
391 std::string *ErrorStr,
392 int64_t FileSize,
393 struct stat *FileInfo) {
Chris Lattner5df6f8f2010-11-23 04:33:43 +0000394 assert(FilenameEnd[0] == 0);
395 if (FileSystemOpts.WorkingDir.empty())
396 return llvm::MemoryBuffer::getFile(FilenameStart, ErrorStr,
397 FileSize, FileInfo);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000398 llvm::sys::Path FilePath(llvm::StringRef(FilenameStart,
399 FilenameEnd-FilenameStart));
400 FixupRelativePath(FilePath, FileSystemOpts);
401
402 return llvm::MemoryBuffer::getFile(FilePath.c_str(), ErrorStr,
403 FileSize, FileInfo);
404}
405
Chris Lattner5df6f8f2010-11-23 04:33:43 +0000406int FileManager::stat_cached(const char *path, struct stat *buf,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000407 const FileSystemOptions &FileSystemOpts) {
Chris Lattner5df6f8f2010-11-23 04:33:43 +0000408 if (FileSystemOpts.WorkingDir.empty())
409 return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf);
410
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000411 llvm::sys::Path FilePath(path);
412 FixupRelativePath(FilePath, FileSystemOpts);
413
414 return StatCache.get() ? StatCache->stat(FilePath.c_str(), buf)
415 : stat(FilePath.c_str(), buf);
416}
417
418void FileManager::FixupRelativePath(llvm::sys::Path &path,
419 const FileSystemOptions &FSOpts) {
420 if (!FSOpts.WorkingDir.empty() && !path.isAbsolute()) {
421 llvm::sys::Path NewPath(FSOpts.WorkingDir);
422 NewPath.appendComponent(path.str());
423 path = NewPath;
424 }
425}
426
Chris Lattner22eb9722006-06-18 05:43:12 +0000427void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000428 llvm::errs() << "\n*** File Manager Stats:\n";
429 llvm::errs() << UniqueFiles.size() << " files found, "
430 << UniqueDirs.size() << " dirs found.\n";
431 llvm::errs() << NumDirLookups << " dir lookups, "
432 << NumDirCacheMisses << " dir cache misses.\n";
433 llvm::errs() << NumFileLookups << " file lookups, "
434 << NumFileCacheMisses << " file cache misses.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000435
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000436 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000437}
Douglas Gregorc5046832009-04-27 18:38:38 +0000438
439int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
Douglas Gregord2eb58a2009-10-16 18:18:30 +0000440 int result = StatSysCallCache::stat(path, buf);
441
Daniel Dunbar2f1a6c12009-12-11 00:27:20 +0000442 // Do not cache failed stats, it is easy to construct common inconsistent
443 // situations if we do, and they are not important for PCH performance (which
444 // currently only needs the stats to construct the initial FileManager
445 // entries).
446 if (result != 0)
447 return result;
448
449 // Cache file 'stat' results and directories with absolutely paths.
450 if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute())
Douglas Gregorc5046832009-04-27 18:38:38 +0000451 StatCalls[path] = StatResult(result, *buf);
Mike Stump11289f42009-09-09 15:08:12 +0000452
453 return result;
Douglas Gregorc5046832009-04-27 18:38:38 +0000454}