blob: 4484a0d23e0fe09cd07ba90e35f3317951f918df [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"
Chris Lattner2f4a89a2006-10-30 03:55:17 +000021#include "llvm/ADT/SmallString.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000022#include "llvm/System/Path.h"
Ted Kremenekd87eef82008-02-24 03:15:25 +000023#include "llvm/Config/config.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000024using namespace clang;
25
26// FIXME: Enhance libsystem to support inode and other fields.
27#include <sys/stat.h>
28
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000029#if defined(_MSC_VER)
Chris Lattner882018b2009-02-12 01:37:35 +000030#define S_ISDIR(s) (_S_IFDIR & s)
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000031#endif
Chris Lattneraf653752006-10-30 03:06:54 +000032
Ted Kremenek8d71e252008-01-11 20:42:05 +000033/// NON_EXISTENT_DIR - A special value distinct from null that is used to
Chris Lattneraf653752006-10-30 03:06:54 +000034/// represent a dir name that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +000035#define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
Chris Lattneraf653752006-10-30 03:06:54 +000036
Ted Kremenek5c04bd82009-01-28 00:27:31 +000037//===----------------------------------------------------------------------===//
38// Windows.
39//===----------------------------------------------------------------------===//
40
Ted Kremenekd87eef82008-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 Kremenek1502b7e2009-01-28 01:01:07 +000078 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
Ted Kremenekd87eef82008-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 Kremenek5c04bd82009-01-28 00:27:31 +000092//===----------------------------------------------------------------------===//
93// Unix-like Systems.
94//===----------------------------------------------------------------------===//
95
Ted Kremenekd87eef82008-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 Kremenek5d7e2e12009-02-12 03:17:57 +0000123 StatBuf.st_ino,
124 StatBuf.st_mode)).first);
Ted Kremenekd87eef82008-02-24 03:15:25 +0000125 }
126
127 size_t size() { return UniqueFiles.size(); }
128};
129
130#endif
131
Ted Kremenek5c04bd82009-01-28 00:27:31 +0000132//===----------------------------------------------------------------------===//
133// Common logic.
134//===----------------------------------------------------------------------===//
Ted Kremenekd87eef82008-02-24 03:15:25 +0000135
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000136FileManager::FileManager()
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000137 : UniqueDirs(*new UniqueDirContainer),
138 UniqueFiles(*new UniqueFileContainer),
Ted Kremenek5d7e2e12009-02-12 03:17:57 +0000139 DirEntries(64), FileEntries(64), NextFileUID(0) {
Ted Kremenekd87eef82008-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
Chris Lattner22eb9722006-06-18 05:43:12 +0000149/// getDirectory - Lookup, cache, and verify the specified directory. This
150/// returns null if the directory doesn't exist.
151///
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000152const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
153 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000154 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +0000155 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000156 DirEntries.GetOrCreateValue(NameStart, NameEnd);
Chris Lattneraf653752006-10-30 03:06:54 +0000157
Chris Lattner22eb9722006-06-18 05:43:12 +0000158 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000159 if (NamedDirEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000160 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000161 ? 0 : NamedDirEnt.getValue();
Chris Lattner22eb9722006-06-18 05:43:12 +0000162
163 ++NumDirCacheMisses;
164
Chris Lattneraf653752006-10-30 03:06:54 +0000165 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000166 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Chris Lattner22eb9722006-06-18 05:43:12 +0000167
Chris Lattner43fd42e2006-10-30 03:40:58 +0000168 // Get the null-terminated directory name as stored as the key of the
169 // DirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000170 const char *InterndDirName = NamedDirEnt.getKeyData();
Chris Lattner43fd42e2006-10-30 03:40:58 +0000171
Chris Lattneraf653752006-10-30 03:06:54 +0000172 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +0000173 struct stat StatBuf;
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000174 if (stat_cached(InterndDirName, &StatBuf) || // Error stat'ing.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000175 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Chris Lattner22eb9722006-06-18 05:43:12 +0000176 return 0;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000177
Chris Lattner22eb9722006-06-18 05:43:12 +0000178 // It exists. See if we have already opened a directory with the same inode.
Ted Kremeneka51c88c2007-12-18 20:45:25 +0000179 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000180 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Chris Lattner22eb9722006-06-18 05:43:12 +0000181
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000182 NamedDirEnt.setValue(&UDE);
183 if (UDE.getName()) // Already have an entry with this inode, return it.
184 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000185
Chris Lattnera85cbe22006-10-30 03:11:40 +0000186 // Otherwise, we don't have this directory yet, add it. We use the string
187 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +0000188 UDE.Name = InterndDirName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000189 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000190}
191
Ted Kremenek8d71e252008-01-11 20:42:05 +0000192/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000193/// represent a filename that doesn't exist on the disk.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000194#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000195
Chris Lattner22eb9722006-06-18 05:43:12 +0000196/// getFile - Lookup, cache, and verify the specified file. This returns null
197/// if the file doesn't exist.
198///
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000199const FileEntry *FileManager::getFile(const char *NameStart,
200 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000201 ++NumFileLookups;
202
203 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000204 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000205 FileEntries.GetOrCreateValue(NameStart, NameEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000206
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000207 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000208 if (NamedFileEnt.getValue())
Ted Kremenek8d71e252008-01-11 20:42:05 +0000209 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000210 ? 0 : NamedFileEnt.getValue();
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000211
Chris Lattner22eb9722006-06-18 05:43:12 +0000212 ++NumFileCacheMisses;
213
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000214 // By default, initialize it to invalid.
Ted Kremenek8d71e252008-01-11 20:42:05 +0000215 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000216
Chris Lattner9c59bda2006-10-30 04:34:28 +0000217 // Figure out what directory it is in. If the string contains a / in it,
218 // strip off everything after it.
Chris Lattner22eb9722006-06-18 05:43:12 +0000219 // FIXME: this logic should be in sys::Path.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000220 const char *SlashPos = NameEnd-1;
Ted Kremenekd87eef82008-02-24 03:15:25 +0000221 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000222 --SlashPos;
Chris Lattner91f5ff62009-08-12 17:50:39 +0000223 // Ignore duplicate //'s.
224 while (SlashPos > NameStart && IS_DIR_SEPARATOR_CHAR(SlashPos[-1]))
225 --SlashPos;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000226
Chris Lattner9c59bda2006-10-30 04:34:28 +0000227 const DirectoryEntry *DirInfo;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000228 if (SlashPos < NameStart) {
229 // Use the current directory if file has no path component.
Chris Lattner9c59bda2006-10-30 04:34:28 +0000230 const char *Name = ".";
231 DirInfo = getDirectory(Name, Name+1);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000232 } else if (SlashPos == NameEnd-1)
Chris Lattner22eb9722006-06-18 05:43:12 +0000233 return 0; // If filename ends with a /, it's a directory.
234 else
Chris Lattner9c59bda2006-10-30 04:34:28 +0000235 DirInfo = getDirectory(NameStart, SlashPos);
236
Chris Lattner22eb9722006-06-18 05:43:12 +0000237 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
238 return 0;
239
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000240 // Get the null-terminated file name as stored as the key of the
241 // FileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000242 const char *InterndFileName = NamedFileEnt.getKeyData();
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000243
Chris Lattner22eb9722006-06-18 05:43:12 +0000244 // 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 Kramer89b422c2009-08-23 12:08:50 +0000249 //llvm::errs() << "STATING: " << Filename;
Ted Kremenekc8b740e2009-02-12 00:39:05 +0000250 if (stat_cached(InterndFileName, &StatBuf) || // Error stat'ing.
251 S_ISDIR(StatBuf.st_mode)) { // A directory?
Chris Lattner81500bc2006-07-19 03:40:07 +0000252 // If this file doesn't exist, we leave a null in FileEntries for this path.
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000253 //llvm::errs() << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000254 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000255 }
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000256 //llvm::errs() << ": exists\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000257
Ted Kremenekf4c38c92007-12-18 22:29:39 +0000258 // It exists. See if we have already opened a file with the same inode.
Chris Lattner22eb9722006-06-18 05:43:12 +0000259 // This occurs when one dir is symlinked to another, for example.
Ted Kremenekd87eef82008-02-24 03:15:25 +0000260 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Chris Lattner22eb9722006-06-18 05:43:12 +0000261
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000262 NamedFileEnt.setValue(&UFE);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000263 if (UFE.getName()) // Already have an entry with this inode, return it.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000264 return &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000265
Chris Lattner22eb9722006-06-18 05:43:12 +0000266 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000267 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
268 // key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000269 UFE.Name = InterndFileName;
270 UFE.Size = StatBuf.st_size;
271 UFE.ModTime = StatBuf.st_mtime;
272 UFE.Dir = DirInfo;
273 UFE.UID = NextFileUID++;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000274 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000275}
276
277void FileManager::PrintStats() const {
Benjamin Kramer89b422c2009-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";
Chris Lattner22eb9722006-06-18 05:43:12 +0000285
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000286 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
Chris Lattner22eb9722006-06-18 05:43:12 +0000287}
Douglas Gregorc5046832009-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}