blob: 4484a0d23e0fe09cd07ba90e35f3317951f918df [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"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000022#include "llvm/System/Path.h"
Ted Kremenek6bb816a2008-02-24 03:15:25 +000023#include "llvm/Config/config.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26// FIXME: Enhance libsystem to support inode and other fields.
27#include <sys/stat.h>
28
Chris Lattnera8c11c62007-09-03 18:37:14 +000029#if defined(_MSC_VER)
Chris Lattner3102c832009-02-12 01:37:35 +000030#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnera8c11c62007-09-03 18:37:14 +000031#endif
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000033/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +000034/// represent a dir name that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +000035#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +000036
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000037//===----------------------------------------------------------------------===//
38// Windows.
39//===----------------------------------------------------------------------===//
40
Ted Kremenek6bb816a2008-02-24 03:15:25 +000041#ifdef LLVM_ON_WIN32
42
43#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
44
45namespace {
46 static std::string GetFullPath(const char *relPath)
47 {
48 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
49 assert(absPathStrPtr && "_fullpath() returned NULL!");
50
51 std::string absPath(absPathStrPtr);
52
53 free(absPathStrPtr);
54 return absPath;
55 }
56}
57
58class FileManager::UniqueDirContainer {
59 /// UniqueDirs - Cache from full path to existing directories/files.
60 ///
61 llvm::StringMap<DirectoryEntry> UniqueDirs;
62
63public:
64 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
65 std::string FullPath(GetFullPath(Name));
66 return UniqueDirs.GetOrCreateValue(
67 FullPath.c_str(),
68 FullPath.c_str() + FullPath.size()
69 ).getValue();
70 }
71
72 size_t size() { return UniqueDirs.size(); }
73};
74
75class FileManager::UniqueFileContainer {
76 /// UniqueFiles - Cache from full path to existing directories/files.
77 ///
Ted Kremenek75368892009-01-28 01:01:07 +000078 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenek6bb816a2008-02-24 03:15:25 +000079
80public:
81 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
82 std::string FullPath(GetFullPath(Name));
83 return UniqueFiles.GetOrCreateValue(
84 FullPath.c_str(),
85 FullPath.c_str() + FullPath.size()
86 ).getValue();
87 }
88
89 size_t size() { return UniqueFiles.size(); }
90};
91
Ted Kremenekcb8d58b2009-01-28 00:27:31 +000092//===----------------------------------------------------------------------===//
93// Unix-like Systems.
94//===----------------------------------------------------------------------===//
95
Ted Kremenek6bb816a2008-02-24 03:15:25 +000096#else
97
98#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
99
100class FileManager::UniqueDirContainer {
101 /// UniqueDirs - Cache from ID's to existing directories/files.
102 ///
103 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
104
105public:
106 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
107 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
108 }
109
110 size_t size() { return UniqueDirs.size(); }
111};
112
113class FileManager::UniqueFileContainer {
114 /// UniqueFiles - Cache from ID's to existing directories/files.
115 ///
116 std::set<FileEntry> UniqueFiles;
117
118public:
119 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
120 return
121 const_cast<FileEntry&>(
122 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
Ted Kremenek96438f32009-02-12 03:17:57 +0000123 StatBuf.st_ino,
124 StatBuf.st_mode)).first);
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000125 }
126
127 size_t size() { return UniqueFiles.size(); }
128};
129
130#endif
131
Ted Kremenekcb8d58b2009-01-28 00:27:31 +0000132//===----------------------------------------------------------------------===//
133// Common logic.
134//===----------------------------------------------------------------------===//
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000135
Ted Kremenek96438f32009-02-12 03:17:57 +0000136FileManager::FileManager()
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000137 : UniqueDirs(*new UniqueDirContainer),
138 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek96438f32009-02-12 03:17:57 +0000139 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000140 NumDirLookups = NumFileLookups = 0;
141 NumDirCacheMisses = NumFileCacheMisses = 0;
142}
143
144FileManager::~FileManager() {
145 delete &UniqueDirs;
146 delete &UniqueFiles;
147}
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149/// getDirectory - Lookup, cache, and verify the specified directory. This
150/// returns null if the directory doesn't exist.
151///
152const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
153 const char *NameEnd) {
154 ++NumDirLookups;
155 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
156 DirEntries.GetOrCreateValue(NameStart, NameEnd);
157
158 // See if there is already an entry in the map.
159 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000160 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 ? 0 : NamedDirEnt.getValue();
162
163 ++NumDirCacheMisses;
164
165 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000166 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
168 // Get the null-terminated directory name as stored as the key of the
169 // DirEntries map.
170 const char *InterndDirName = NamedDirEnt.getKeyData();
171
172 // Check to see if the directory exists.
173 struct stat StatBuf;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000174 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
176 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // It exists. See if we have already opened a directory with the same inode.
Ted Kremenekda995442007-12-18 20:45:25 +0000179 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000180 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
182 NamedDirEnt.setValue(&UDE);
183 if (UDE.getName()) // Already have an entry with this inode, return it.
184 return &UDE;
185
186 // Otherwise, we don't have this directory yet, add it. We use the string
187 // key from the DirEntries map as the string.
188 UDE.Name = InterndDirName;
189 return &UDE;
190}
191
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000192/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000193/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000194#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000195
196/// getFile - Lookup, cache, and verify the specified file. This returns null
197/// if the file doesn't exist.
198///
199const FileEntry *FileManager::getFile(const char *NameStart,
200 const char *NameEnd) {
201 ++NumFileLookups;
202
203 // See if there is already an entry in the map.
204 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
205 FileEntries.GetOrCreateValue(NameStart, NameEnd);
206
207 // See if there is already an entry in the map.
208 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000209 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 ? 0 : NamedFileEnt.getValue();
211
212 ++NumFileCacheMisses;
213
214 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000215 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
217 // Figure out what directory it is in. If the string contains a / in it,
218 // strip off everything after it.
219 // FIXME: this logic should be in sys::Path.
220 const char *SlashPos = NameEnd-1;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000221 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 --SlashPos;
Chris Lattner46730b22009-08-12 17:50:39 +0000223 // Ignore duplicate //'s.
224 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
225 --SlashPos;
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
227 const DirectoryEntry *DirInfo;
228 if (SlashPos < NameStart) {
229 // Use the current directory if file has no path component.
230 const char *Name = ".";
231 DirInfo = getDirectory(Name, Name+1);
232 } else if (SlashPos == NameEnd-1)
233 return 0; // If filename ends with a /, it's a directory.
234 else
235 DirInfo = getDirectory(NameStart, SlashPos);
236
237 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
238 return 0;
239
240 // Get the null-terminated file name as stored as the key of the
241 // FileEntries map.
242 const char *InterndFileName = NamedFileEnt.getKeyData();
243
244 // FIXME: Use the directory info to prune this, before doing the stat syscall.
245 // FIXME: This will reduce the # syscalls.
246
247 // Nope, there isn't. Check to see if the file exists.
248 struct stat StatBuf;
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000249 //llvm::errs() << "STATING: " << Filename;
Ted Kremenekfc7052d2009-02-12 00:39:05 +0000250 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
251 S_ISDIR(StatBuf.st_mode)) { // A directory?
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000253 //llvm::errs() << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 return 0;
255 }
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000256 //llvm::errs() << ": exists\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257
Ted Kremenekbca6d122007-12-18 22:29:39 +0000258 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000260 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000261
262 NamedFileEnt.setValue(&UFE);
263 if (UFE.getName()) // Already have an entry with this inode, return it.
264 return &UFE;
265
266 // Otherwise, we don't have this directory yet, add it.
267 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
268 // key.
269 UFE.Name = InterndFileName;
270 UFE.Size = StatBuf.st_size;
271 UFE.ModTime = StatBuf.st_mtime;
272 UFE.Dir = DirInfo;
273 UFE.UID = NextFileUID++;
274 return &UFE;
275}
276
277void FileManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000278 llvm::errs() << "\n*** File Manager Stats:\n";
279 llvm::errs() << UniqueFiles.size() << " files found, "
280 << UniqueDirs.size() << " dirs found.\n";
281 llvm::errs() << NumDirLookups << " dir lookups, "
282 << NumDirCacheMisses << " dir cache misses.\n";
283 llvm::errs() << NumFileLookups << " file lookups, "
284 << NumFileCacheMisses << " file cache misses.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000285
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000286 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000288
289int MemorizeStatCalls::stat(const char *path, struct stat *buf) {
290 int result = ::stat(path, buf);
291
292 if (result != 0) {
293 // Cache failed 'stat' results.
294 struct stat empty;
295 StatCalls[path] = StatResult(result, empty);
296 }
297 else if (!S_ISDIR(buf->st_mode) || llvm::sys::Path(path).isAbsolute()) {
298 // Cache file 'stat' results and directories with absolutely
299 // paths.
300 StatCalls[path] = StatResult(result, *buf);
301 }
302
303 return result;
304}