blob: cfc08ed084b1f87588272b0a7547b9a8e0c6a124 [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)
32#define S_ISDIR(s) (_S_IFDIR & s)
33#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 Kremenek6bb816a2008-02-24 03:15:25 +000039#ifdef LLVM_ON_WIN32
40
41#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
42
43namespace {
44 static std::string GetFullPath(const char *relPath)
45 {
46 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
47 assert(absPathStrPtr && "_fullpath() returned NULL!");
48
49 std::string absPath(absPathStrPtr);
50
51 free(absPathStrPtr);
52 return absPath;
53 }
54}
55
56class FileManager::UniqueDirContainer {
57 /// UniqueDirs - Cache from full path to existing directories/files.
58 ///
59 llvm::StringMap<DirectoryEntry> UniqueDirs;
60
61public:
62 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
63 std::string FullPath(GetFullPath(Name));
64 return UniqueDirs.GetOrCreateValue(
65 FullPath.c_str(),
66 FullPath.c_str() + FullPath.size()
67 ).getValue();
68 }
69
70 size_t size() { return UniqueDirs.size(); }
71};
72
73class FileManager::UniqueFileContainer {
74 /// UniqueFiles - Cache from full path to existing directories/files.
75 ///
76 llvm::StringMap<FileEntry> UniqueFiles;
77
78public:
79 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
80 std::string FullPath(GetFullPath(Name));
81 return UniqueFiles.GetOrCreateValue(
82 FullPath.c_str(),
83 FullPath.c_str() + FullPath.size()
84 ).getValue();
85 }
86
87 size_t size() { return UniqueFiles.size(); }
88};
89
90#else
91
92#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
93
94class FileManager::UniqueDirContainer {
95 /// UniqueDirs - Cache from ID's to existing directories/files.
96 ///
97 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
98
99public:
100 DirectoryEntry &getDirectory(const char *Name, struct stat &StatBuf) {
101 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
102 }
103
104 size_t size() { return UniqueDirs.size(); }
105};
106
107class FileManager::UniqueFileContainer {
108 /// UniqueFiles - Cache from ID's to existing directories/files.
109 ///
110 std::set<FileEntry> UniqueFiles;
111
112public:
113 FileEntry &getFile(const char *Name, struct stat &StatBuf) {
114 return
115 const_cast<FileEntry&>(
116 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
117 StatBuf.st_ino)).first);
118 }
119
120 size_t size() { return UniqueFiles.size(); }
121};
122
123#endif
124
125
126FileManager::FileManager() : UniqueDirs(*new UniqueDirContainer),
127 UniqueFiles(*new UniqueFileContainer),
128 DirEntries(64), FileEntries(64), NextFileUID(0)
129{
130 NumDirLookups = NumFileLookups = 0;
131 NumDirCacheMisses = NumFileCacheMisses = 0;
132}
133
134FileManager::~FileManager() {
135 delete &UniqueDirs;
136 delete &UniqueFiles;
137}
138
139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140/// getDirectory - Lookup, cache, and verify the specified directory. This
141/// returns null if the directory doesn't exist.
142///
143const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
144 const char *NameEnd) {
145 ++NumDirLookups;
146 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
147 DirEntries.GetOrCreateValue(NameStart, NameEnd);
148
149 // See if there is already an entry in the map.
150 if (NamedDirEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000151 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ? 0 : NamedDirEnt.getValue();
153
154 ++NumDirCacheMisses;
155
156 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000157 NamedDirEnt.setValue(NON_EXISTENT_DIR);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158
159 // Get the null-terminated directory name as stored as the key of the
160 // DirEntries map.
161 const char *InterndDirName = NamedDirEnt.getKeyData();
162
163 // Check to see if the directory exists.
164 struct stat StatBuf;
165 if (stat(InterndDirName, &StatBuf) || // Error stat'ing.
166 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
167 return 0;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 // It exists. See if we have already opened a directory with the same inode.
Ted Kremenekda995442007-12-18 20:45:25 +0000170 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000171 DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172
173 NamedDirEnt.setValue(&UDE);
174 if (UDE.getName()) // Already have an entry with this inode, return it.
175 return &UDE;
176
177 // Otherwise, we don't have this directory yet, add it. We use the string
178 // key from the DirEntries map as the string.
179 UDE.Name = InterndDirName;
180 return &UDE;
181}
182
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000183/// NON_EXISTENT_FILE - A special value distinct from null that is used to
Reid Spencer5f016e22007-07-11 17:01:13 +0000184/// represent a filename that doesn't exist on the disk.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000185#define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000186
187/// getFile - Lookup, cache, and verify the specified file. This returns null
188/// if the file doesn't exist.
189///
190const FileEntry *FileManager::getFile(const char *NameStart,
191 const char *NameEnd) {
192 ++NumFileLookups;
193
194 // See if there is already an entry in the map.
195 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
196 FileEntries.GetOrCreateValue(NameStart, NameEnd);
197
198 // See if there is already an entry in the map.
199 if (NamedFileEnt.getValue())
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000200 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 ? 0 : NamedFileEnt.getValue();
202
203 ++NumFileCacheMisses;
204
205 // By default, initialize it to invalid.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000206 NamedFileEnt.setValue(NON_EXISTENT_FILE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
208 // Figure out what directory it is in. If the string contains a / in it,
209 // strip off everything after it.
210 // FIXME: this logic should be in sys::Path.
211 const char *SlashPos = NameEnd-1;
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000212 while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 --SlashPos;
214
215 const DirectoryEntry *DirInfo;
216 if (SlashPos < NameStart) {
217 // Use the current directory if file has no path component.
218 const char *Name = ".";
219 DirInfo = getDirectory(Name, Name+1);
220 } else if (SlashPos == NameEnd-1)
221 return 0; // If filename ends with a /, it's a directory.
222 else
223 DirInfo = getDirectory(NameStart, SlashPos);
224
225 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
226 return 0;
227
228 // Get the null-terminated file name as stored as the key of the
229 // FileEntries map.
230 const char *InterndFileName = NamedFileEnt.getKeyData();
231
232 // FIXME: Use the directory info to prune this, before doing the stat syscall.
233 // FIXME: This will reduce the # syscalls.
234
235 // Nope, there isn't. Check to see if the file exists.
236 struct stat StatBuf;
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000237 //llvm::cerr << "STATING: " << Filename;
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 if (stat(InterndFileName, &StatBuf) || // Error stat'ing.
239 S_ISDIR(StatBuf.st_mode)) { // A directory?
240 // If this file doesn't exist, we leave a null in FileEntries for this path.
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000241 //llvm::cerr << ": Not existing\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 return 0;
243 }
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000244 //llvm::cerr << ": exists\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
Ted Kremenekbca6d122007-12-18 22:29:39 +0000246 // It exists. See if we have already opened a file with the same inode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // This occurs when one dir is symlinked to another, for example.
Ted Kremenek6bb816a2008-02-24 03:15:25 +0000248 FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 NamedFileEnt.setValue(&UFE);
251 if (UFE.getName()) // Already have an entry with this inode, return it.
252 return &UFE;
253
254 // Otherwise, we don't have this directory yet, add it.
255 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
256 // key.
257 UFE.Name = InterndFileName;
258 UFE.Size = StatBuf.st_size;
259 UFE.ModTime = StatBuf.st_mtime;
260 UFE.Dir = DirInfo;
261 UFE.UID = NextFileUID++;
262 return &UFE;
263}
264
265void FileManager::PrintStats() const {
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000266 llvm::cerr << "\n*** File Manager Stats:\n";
267 llvm::cerr << UniqueFiles.size() << " files found, "
268 << UniqueDirs.size() << " dirs found.\n";
269 llvm::cerr << NumDirLookups << " dir lookups, "
270 << NumDirCacheMisses << " dir cache misses.\n";
271 llvm::cerr << NumFileLookups << " file lookups, "
272 << NumFileCacheMisses << " file cache misses.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000273
Ted Kremenek3d2da3d2008-01-11 20:42:05 +0000274 //llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}