blob: 773d36667562e7c7ef0d4895804139e2ecf4ee35 [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"
21#include "llvm/ADT/SmallString.h"
Ted Kremenek9551a2c2007-12-04 18:21:35 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000024#include "llvm/Support/Streams.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000025#include "llvm/Config/config.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28// FIXME: Enhance libsystem to support inode and other fields.
29#include <sys/stat.h>
30
Chris Lattnera8c11c62007-09-03 18:37:14 +000031#if defined(_MSC_VER)
Chris Lattner3102c832009-02-12 01:37:35 +000032#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnera8c11c62007-09-03 18:37:14 +000033#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000034
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000035/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000036/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000037#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000039//===----------------------------------------------------------------------===//
40// Windows.
41//===----------------------------------------------------------------------===//
42
Ted Kremenek6bb816a2008-02-24 03:15:25 +000043#ifdef LLVM_ON_WIN32
44
45#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
46
47namespace {
48 static std::string GetFullPath(const char *relPath)
49 {
50 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
51 assert(absPathStrPtr && "_fullpath() returned NULL!");
52
53 std::string absPath(absPathStrPtr);
54
55 free(absPathStrPtr);
56 return absPath;
57 }
58}
59
60class FileManager::UniqueDirContainer {
61 /// UniqueDirs - Cache from full path to existing directories/files.
62 ///
63 llvm::StringMap<DirectoryEntry> UniqueDirs;
64
65public:
66 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
67 std::string FullPath(GetFullPath(Name));
68 return UniqueDirs.GetOrCreateValue(
69 FullPath.c_str(),
70 FullPath.c_str() + FullPath.size()
71 ).getValue();
72 }
73
74 size_t size() { return UniqueDirs.size(); }
75};
76
77class FileManager::UniqueFileContainer {
78 /// UniqueFiles - Cache from full path to existing directories/files.
79 ///
Ted Kremenek75368892009-01-28 01:01:07 +000080 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000081
82public:
83 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
84 std::string FullPath(GetFullPath(Name));
85 return UniqueFiles.GetOrCreateValue(
86 FullPath.c_str(),
87 FullPath.c_str() + FullPath.size()
88 ).getValue();
89 }
90
91 size_t size() { return UniqueFiles.size(); }
92};
93
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000094//===----------------------------------------------------------------------===//
95// Unix-like Systems.
96//===----------------------------------------------------------------------===//
97
Ted Kremenek6bb816a2008-02-24 03:15:25 +000098#else
99
100#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
101
102class FileManager::UniqueDirContainer {
103 /// UniqueDirs - Cache from ID's to existing directories/files.
104 ///
105 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
106
107public:
108 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
109 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
110 }
111
112 size_t size() { return UniqueDirs.size(); }
113};
114
115class FileManager::UniqueFileContainer {
116 /// UniqueFiles - Cache from ID's to existing directories/files.
117 ///
118 std::set<FileEntry> UniqueFiles;
119
120public:
121 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
122 return
123 const_cast<FileEntry&>(
124 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000125 StatBuf.st_ino,
126 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000127 }
128
129 size_t size() { return UniqueFiles.size(); }
130};
131
132#endif
133
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000134//===----------------------------------------------------------------------===//
135// Common logic.
136//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000137
Ted Kremenek96438f32009-02-12 03:17:57 +0000138FileManager::FileManager()
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000139 : UniqueDirs(*new UniqueDirContainer),
140 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek96438f32009-02-12 03:17:57 +0000141 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000142 NumDirLookups = NumFileLookups = 0;
143 NumDirCacheMisses = NumFileCacheMisses = 0;
144}
145
146FileManager::~FileManager() {
147 delete &UniqueDirs;
148 delete &UniqueFiles;
149}
150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151/// getDirectory - Lookup, cache, and verify the specified directory. This
152/// returns null if the directory doesn't exist.
153///
154const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
155 const char *NameEnd) {
156 ++NumDirLookups;
157 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
158 DirEntries.GetOrCreateValue(NameStart, NameEnd);
159
160 // See if there is already an entry in the map.
161 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000162 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ? 0 : NamedDirEnt.getValue();
164
165 ++NumDirCacheMisses;
166
167 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000168 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169
170 // Get the null-terminated directory name as stored as the key of the
171 // DirEntries map.
172 const char *InterndDirName = NamedDirEnt.getKeyData();
173
174 // Check to see if the directory exists.
175 struct stat StatBuf;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000176 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
178 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 // It exists. See if we have already opened a directory with the same inode.
Ted Kremenekda995442007-12-18 20:45:25 +0000181 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000182 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183
184 NamedDirEnt.setValue(&UDE);
185 if (UDE.getName()) // Already have an entry with this inode, return it.
186 return &UDE;
187
188 // Otherwise, we don't have this directory yet, add it. We use the string
189 // key from the DirEntries map as the string.
190 UDE.Name = InterndDirName;
191 return &UDE;
192}
193
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000194/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000195/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000196#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000197
198/// getFile - Lookup, cache, and verify the specified file. This returns null
199/// if the file doesn't exist.
200///
201const FileEntry *FileManager::getFile(const char *NameStart,
202 const char *NameEnd) {
203 ++NumFileLookups;
204
205 // See if there is already an entry in the map.
206 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
207 FileEntries.GetOrCreateValue(NameStart, NameEnd);
208
209 // See if there is already an entry in the map.
210 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000211 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 ? 0 : NamedFileEnt.getValue();
213
214 ++NumFileCacheMisses;
215
216 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000217 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
219 // Figure out what directory it is in. If the string contains a / in it,
220 // strip off everything after it.
221 // FIXME: this logic should be in sys::Path.
222 const char *SlashPos = NameEnd-1;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000223 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 --SlashPos;
225
226 const DirectoryEntry *DirInfo;
227 if (SlashPos < NameStart) {
228 // Use the current directory if file has no path component.
229 const char *Name = ".";
230 DirInfo = getDirectory(Name, Name+1);
231 } else if (SlashPos == NameEnd-1)
232 return 0; // If filename ends with a /, it's a directory.
233 else
234 DirInfo = getDirectory(NameStart, SlashPos);
235
236 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
237 return 0;
238
239 // Get the null-terminated file name as stored as the key of the
240 // FileEntries map.
241 const char *InterndFileName = NamedFileEnt.getKeyData();
242
243 // FIXME: Use the directory info to prune this, before doing the stat syscall.
244 // FIXME: This will reduce the # syscalls.
245
246 // Nope, there isn't. Check to see if the file exists.
247 struct stat StatBuf;
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000248 //llvm::cerr << "STATING: " << Filename;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000249 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
250 S_ISDIR(StatBuf.st_mode)) { // A directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // If this file doesn't exist, we leave a null in FileEntries for this path.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000252 //llvm::cerr << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 return 0;
254 }
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000255 //llvm::cerr << ": exists\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
Ted Kremenekbca6d122007-12-18 22:29:39 +0000257 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000259 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000260
261 NamedFileEnt.setValue(&UFE);
262 if (UFE.getName()) // Already have an entry with this inode, return it.
263 return &UFE;
264
265 // Otherwise, we don't have this directory yet, add it.
266 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
267 // key.
268 UFE.Name = InterndFileName;
269 UFE.Size = StatBuf.st_size;
270 UFE.ModTime = StatBuf.st_mtime;
271 UFE.Dir = DirInfo;
272 UFE.UID = NextFileUID++;
273 return &UFE;
274}
275
276void FileManager::PrintStats() const {
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000277 llvm::cerr << "\n*** File Manager Stats:\n";
278 llvm::cerr << UniqueFiles.size() << " files found, "
279 << UniqueDirs.size() << " dirs found.\n";
280 llvm::cerr << NumDirLookups << " dir lookups, "
281 << NumDirCacheMisses << " dir cache misses.\n";
282 llvm::cerr << NumFileLookups << " file lookups, "
283 << NumFileCacheMisses << " file cache misses.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000285 //llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000286}