[analyzer] Use the new registration mechanism on the non-path-sensitive-checkers:
[clang.git] / lib / Basic / FileManager.cpp
blob342413d7da52de58e8ac00bc8da4e83ce92718ae
1 //===--- FileManager.cpp - File System Probing and Caching ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the FileManager interface.
12 //===----------------------------------------------------------------------===//
14 // TODO: This should index all interesting directories with dirent calls.
15 // getdirentries ?
16 // opendir/readdir_r/closedir ?
18 //===----------------------------------------------------------------------===//
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Basic/FileSystemStatCache.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/system_error.h"
29 #include "llvm/Config/config.h"
30 #include <map>
31 #include <set>
32 #include <string>
34 // FIXME: This is terrible, we need this for ::close.
35 #if !defined(_MSC_VER) && !defined(__MINGW32__)
36 #include <unistd.h>
37 #include <sys/uio.h>
38 #else
39 #include <io.h>
40 #endif
41 using namespace clang;
43 // FIXME: Enhance libsystem to support inode and other fields.
44 #include <sys/stat.h>
46 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
47 /// represent a dir name that doesn't exist on the disk.
48 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
50 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
51 /// represent a filename that doesn't exist on the disk.
52 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
55 FileEntry::~FileEntry() {
56 // If this FileEntry owns an open file descriptor that never got used, close
57 // it.
58 if (FD != -1) ::close(FD);
61 //===----------------------------------------------------------------------===//
62 // Windows.
63 //===----------------------------------------------------------------------===//
65 #ifdef LLVM_ON_WIN32
67 namespace {
68 static std::string GetFullPath(const char *relPath) {
69 char *absPathStrPtr = _fullpath(NULL, relPath, 0);
70 assert(absPathStrPtr && "_fullpath() returned NULL!");
72 std::string absPath(absPathStrPtr);
74 free(absPathStrPtr);
75 return absPath;
79 class FileManager::UniqueDirContainer {
80 /// UniqueDirs - Cache from full path to existing directories/files.
81 ///
82 llvm::StringMap<DirectoryEntry> UniqueDirs;
84 public:
85 /// getDirectory - Return an existing DirectoryEntry with the given
86 /// name if there is already one; otherwise create and return a
87 /// default-constructed DirectoryEntry.
88 DirectoryEntry &getDirectory(const char *Name,
89 const struct stat & /*StatBuf*/) {
90 std::string FullPath(GetFullPath(Name));
91 return UniqueDirs.GetOrCreateValue(FullPath).getValue();
94 size_t size() const { return UniqueDirs.size(); }
97 class FileManager::UniqueFileContainer {
98 /// UniqueFiles - Cache from full path to existing directories/files.
99 ///
100 llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
102 public:
103 /// getFile - Return an existing FileEntry with the given name if
104 /// there is already one; otherwise create and return a
105 /// default-constructed FileEntry.
106 FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
107 std::string FullPath(GetFullPath(Name));
109 // LowercaseString because Windows filesystem is case insensitive.
110 FullPath = llvm::LowercaseString(FullPath);
111 return UniqueFiles.GetOrCreateValue(FullPath).getValue();
114 size_t size() const { return UniqueFiles.size(); }
117 //===----------------------------------------------------------------------===//
118 // Unix-like Systems.
119 //===----------------------------------------------------------------------===//
121 #else
123 class FileManager::UniqueDirContainer {
124 /// UniqueDirs - Cache from ID's to existing directories/files.
125 std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
127 public:
128 /// getDirectory - Return an existing DirectoryEntry with the given
129 /// ID's if there is already one; otherwise create and return a
130 /// default-constructed DirectoryEntry.
131 DirectoryEntry &getDirectory(const char * /*Name*/,
132 const struct stat &StatBuf) {
133 return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
136 size_t size() const { return UniqueDirs.size(); }
139 class FileManager::UniqueFileContainer {
140 /// UniqueFiles - Cache from ID's to existing directories/files.
141 std::set<FileEntry> UniqueFiles;
143 public:
144 /// getFile - Return an existing FileEntry with the given ID's if
145 /// there is already one; otherwise create and return a
146 /// default-constructed FileEntry.
147 FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
148 return
149 const_cast<FileEntry&>(
150 *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
151 StatBuf.st_ino,
152 StatBuf.st_mode)).first);
155 size_t size() const { return UniqueFiles.size(); }
158 #endif
160 //===----------------------------------------------------------------------===//
161 // Common logic.
162 //===----------------------------------------------------------------------===//
164 FileManager::FileManager(const FileSystemOptions &FSO)
165 : FileSystemOpts(FSO),
166 UniqueRealDirs(*new UniqueDirContainer()),
167 UniqueRealFiles(*new UniqueFileContainer()),
168 SeenDirEntries(64), SeenFileEntries(64), NextFileUID(0) {
169 NumDirLookups = NumFileLookups = 0;
170 NumDirCacheMisses = NumFileCacheMisses = 0;
173 FileManager::~FileManager() {
174 delete &UniqueRealDirs;
175 delete &UniqueRealFiles;
176 for (unsigned i = 0, e = VirtualFileEntries.size(); i != e; ++i)
177 delete VirtualFileEntries[i];
178 for (unsigned i = 0, e = VirtualDirectoryEntries.size(); i != e; ++i)
179 delete VirtualDirectoryEntries[i];
182 void FileManager::addStatCache(FileSystemStatCache *statCache,
183 bool AtBeginning) {
184 assert(statCache && "No stat cache provided?");
185 if (AtBeginning || StatCache.get() == 0) {
186 statCache->setNextStatCache(StatCache.take());
187 StatCache.reset(statCache);
188 return;
191 FileSystemStatCache *LastCache = StatCache.get();
192 while (LastCache->getNextStatCache())
193 LastCache = LastCache->getNextStatCache();
195 LastCache->setNextStatCache(statCache);
198 void FileManager::removeStatCache(FileSystemStatCache *statCache) {
199 if (!statCache)
200 return;
202 if (StatCache.get() == statCache) {
203 // This is the first stat cache.
204 StatCache.reset(StatCache->takeNextStatCache());
205 return;
208 // Find the stat cache in the list.
209 FileSystemStatCache *PrevCache = StatCache.get();
210 while (PrevCache && PrevCache->getNextStatCache() != statCache)
211 PrevCache = PrevCache->getNextStatCache();
213 assert(PrevCache && "Stat cache not found for removal");
214 PrevCache->setNextStatCache(statCache->getNextStatCache());
217 /// \brief Retrieve the directory that the given file name resides in.
218 /// Filename can point to either a real file or a virtual file.
219 static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
220 llvm::StringRef Filename) {
221 if (Filename.empty())
222 return NULL;
224 if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
225 return NULL; // If Filename is a directory.
227 llvm::StringRef DirName = llvm::sys::path::parent_path(Filename);
228 // Use the current directory if file has no path component.
229 if (DirName.empty())
230 DirName = ".";
232 return FileMgr.getDirectory(DirName);
235 /// Add all ancestors of the given path (pointing to either a file or
236 /// a directory) as virtual directories.
237 void FileManager::addAncestorsAsVirtualDirs(llvm::StringRef Path) {
238 llvm::StringRef DirName = llvm::sys::path::parent_path(Path);
239 if (DirName.empty())
240 return;
242 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
243 SeenDirEntries.GetOrCreateValue(DirName);
245 // When caching a virtual directory, we always cache its ancestors
246 // at the same time. Therefore, if DirName is already in the cache,
247 // we don't need to recurse as its ancestors must also already be in
248 // the cache.
249 if (NamedDirEnt.getValue())
250 return;
252 // Add the virtual directory to the cache.
253 DirectoryEntry *UDE = new DirectoryEntry;
254 UDE->Name = NamedDirEnt.getKeyData();
255 NamedDirEnt.setValue(UDE);
256 VirtualDirectoryEntries.push_back(UDE);
258 // Recursively add the other ancestors.
259 addAncestorsAsVirtualDirs(DirName);
262 /// getDirectory - Lookup, cache, and verify the specified directory
263 /// (real or virtual). This returns NULL if the directory doesn't
264 /// exist.
266 const DirectoryEntry *FileManager::getDirectory(llvm::StringRef DirName) {
267 // stat doesn't like trailing separators (at least on Windows).
268 if (DirName.size() > 1 && llvm::sys::path::is_separator(DirName.back()))
269 DirName = DirName.substr(0, DirName.size()-1);
271 ++NumDirLookups;
272 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
273 SeenDirEntries.GetOrCreateValue(DirName);
275 // See if there was already an entry in the map. Note that the map
276 // contains both virtual and real directories.
277 if (NamedDirEnt.getValue())
278 return NamedDirEnt.getValue() == NON_EXISTENT_DIR
279 ? 0 : NamedDirEnt.getValue();
281 ++NumDirCacheMisses;
283 // By default, initialize it to invalid.
284 NamedDirEnt.setValue(NON_EXISTENT_DIR);
286 // Get the null-terminated directory name as stored as the key of the
287 // SeenDirEntries map.
288 const char *InterndDirName = NamedDirEnt.getKeyData();
290 // Check to see if the directory exists.
291 struct stat StatBuf;
292 if (getStatValue(InterndDirName, StatBuf, 0/*directory lookup*/)) {
293 // There's no real directory at the given path.
294 return 0;
297 // It exists. See if we have already opened a directory with the
298 // same inode (this occurs on Unix-like systems when one dir is
299 // symlinked to another, for example) or the same path (on
300 // Windows).
301 DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
303 NamedDirEnt.setValue(&UDE);
304 if (!UDE.getName()) {
305 // We don't have this directory yet, add it. We use the string
306 // key from the SeenDirEntries map as the string.
307 UDE.Name = InterndDirName;
310 return &UDE;
313 /// getFile - Lookup, cache, and verify the specified file (real or
314 /// virtual). This returns NULL if the file doesn't exist.
316 const FileEntry *FileManager::getFile(llvm::StringRef Filename) {
317 ++NumFileLookups;
319 // See if there is already an entry in the map.
320 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
321 SeenFileEntries.GetOrCreateValue(Filename);
323 // See if there is already an entry in the map.
324 if (NamedFileEnt.getValue())
325 return NamedFileEnt.getValue() == NON_EXISTENT_FILE
326 ? 0 : NamedFileEnt.getValue();
328 ++NumFileCacheMisses;
330 // By default, initialize it to invalid.
331 NamedFileEnt.setValue(NON_EXISTENT_FILE);
333 // Get the null-terminated file name as stored as the key of the
334 // SeenFileEntries map.
335 const char *InterndFileName = NamedFileEnt.getKeyData();
337 // Look up the directory for the file. When looking up something like
338 // sys/foo.h we'll discover all of the search directories that have a 'sys'
339 // subdirectory. This will let us avoid having to waste time on known-to-fail
340 // searches when we go to find sys/bar.h, because all the search directories
341 // without a 'sys' subdir will get a cached failure result.
342 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
343 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
344 return 0;
346 // FIXME: Use the directory info to prune this, before doing the stat syscall.
347 // FIXME: This will reduce the # syscalls.
349 // Nope, there isn't. Check to see if the file exists.
350 int FileDescriptor = -1;
351 struct stat StatBuf;
352 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor)) {
353 // There's no real file at the given path.
354 return 0;
357 // It exists. See if we have already opened a file with the same inode.
358 // This occurs when one dir is symlinked to another, for example.
359 FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
361 NamedFileEnt.setValue(&UFE);
362 if (UFE.getName()) { // Already have an entry with this inode, return it.
363 // If the stat process opened the file, close it to avoid a FD leak.
364 if (FileDescriptor != -1)
365 close(FileDescriptor);
367 return &UFE;
370 // Otherwise, we don't have this directory yet, add it.
371 // FIXME: Change the name to be a char* that points back to the
372 // 'SeenFileEntries' key.
373 UFE.Name = InterndFileName;
374 UFE.Size = StatBuf.st_size;
375 UFE.ModTime = StatBuf.st_mtime;
376 UFE.Dir = DirInfo;
377 UFE.UID = NextFileUID++;
378 UFE.FD = FileDescriptor;
379 return &UFE;
382 const FileEntry *
383 FileManager::getVirtualFile(llvm::StringRef Filename, off_t Size,
384 time_t ModificationTime) {
385 ++NumFileLookups;
387 // See if there is already an entry in the map.
388 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
389 SeenFileEntries.GetOrCreateValue(Filename);
391 // See if there is already an entry in the map.
392 if (NamedFileEnt.getValue() && NamedFileEnt.getValue() != NON_EXISTENT_FILE)
393 return NamedFileEnt.getValue();
395 ++NumFileCacheMisses;
397 // By default, initialize it to invalid.
398 NamedFileEnt.setValue(NON_EXISTENT_FILE);
400 addAncestorsAsVirtualDirs(Filename);
401 FileEntry *UFE = 0;
403 // Now that all ancestors of Filename are in the cache, the
404 // following call is guaranteed to find the DirectoryEntry from the
405 // cache.
406 const DirectoryEntry *DirInfo = getDirectoryFromFile(*this, Filename);
407 assert(DirInfo &&
408 "The directory of a virtual file should already be in the cache.");
410 // Check to see if the file exists. If so, drop the virtual file
411 int FileDescriptor = -1;
412 struct stat StatBuf;
413 const char *InterndFileName = NamedFileEnt.getKeyData();
414 if (getStatValue(InterndFileName, StatBuf, &FileDescriptor) == 0) {
415 // If the stat process opened the file, close it to avoid a FD leak.
416 if (FileDescriptor != -1)
417 close(FileDescriptor);
419 StatBuf.st_size = Size;
420 StatBuf.st_mtime = ModificationTime;
421 UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
423 NamedFileEnt.setValue(UFE);
425 // If we had already opened this file, close it now so we don't
426 // leak the descriptor. We're not going to use the file
427 // descriptor anyway, since this is a virtual file.
428 if (UFE->FD != -1) {
429 close(UFE->FD);
430 UFE->FD = -1;
433 // If we already have an entry with this inode, return it.
434 if (UFE->getName())
435 return UFE;
438 if (!UFE) {
439 UFE = new FileEntry();
440 VirtualFileEntries.push_back(UFE);
441 NamedFileEnt.setValue(UFE);
444 UFE->Name = InterndFileName;
445 UFE->Size = Size;
446 UFE->ModTime = ModificationTime;
447 UFE->Dir = DirInfo;
448 UFE->UID = NextFileUID++;
449 UFE->FD = -1;
450 return UFE;
453 void FileManager::FixupRelativePath(llvm::sys::Path &path,
454 const FileSystemOptions &FSOpts) {
455 if (FSOpts.WorkingDir.empty() || llvm::sys::path::is_absolute(path.str()))
456 return;
458 llvm::SmallString<128> NewPath(FSOpts.WorkingDir);
459 llvm::sys::path::append(NewPath, path.str());
460 path = NewPath;
463 llvm::MemoryBuffer *FileManager::
464 getBufferForFile(const FileEntry *Entry, std::string *ErrorStr) {
465 llvm::OwningPtr<llvm::MemoryBuffer> Result;
466 llvm::error_code ec;
467 if (FileSystemOpts.WorkingDir.empty()) {
468 const char *Filename = Entry->getName();
469 // If the file is already open, use the open file descriptor.
470 if (Entry->FD != -1) {
471 ec = llvm::MemoryBuffer::getOpenFile(Entry->FD, Filename, Result,
472 Entry->getSize());
473 if (ErrorStr)
474 *ErrorStr = ec.message();
476 close(Entry->FD);
477 Entry->FD = -1;
478 return Result.take();
481 // Otherwise, open the file.
482 ec = llvm::MemoryBuffer::getFile(Filename, Result, Entry->getSize());
483 if (ec && ErrorStr)
484 *ErrorStr = ec.message();
485 return Result.take();
488 llvm::sys::Path FilePath(Entry->getName());
489 FixupRelativePath(FilePath, FileSystemOpts);
490 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result, Entry->getSize());
491 if (ec && ErrorStr)
492 *ErrorStr = ec.message();
493 return Result.take();
496 llvm::MemoryBuffer *FileManager::
497 getBufferForFile(llvm::StringRef Filename, std::string *ErrorStr) {
498 llvm::OwningPtr<llvm::MemoryBuffer> Result;
499 llvm::error_code ec;
500 if (FileSystemOpts.WorkingDir.empty()) {
501 ec = llvm::MemoryBuffer::getFile(Filename, Result);
502 if (ec && ErrorStr)
503 *ErrorStr = ec.message();
504 return Result.take();
507 llvm::sys::Path FilePath(Filename);
508 FixupRelativePath(FilePath, FileSystemOpts);
509 ec = llvm::MemoryBuffer::getFile(FilePath.c_str(), Result);
510 if (ec && ErrorStr)
511 *ErrorStr = ec.message();
512 return Result.take();
515 /// getStatValue - Get the 'stat' information for the specified path,
516 /// using the cache to accelerate it if possible. This returns true
517 /// if the path points to a virtual file or does not exist, or returns
518 /// false if it's an existent real file. If FileDescriptor is NULL,
519 /// do directory look-up instead of file look-up.
520 bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
521 int *FileDescriptor) {
522 // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
523 // absolute!
524 if (FileSystemOpts.WorkingDir.empty())
525 return FileSystemStatCache::get(Path, StatBuf, FileDescriptor,
526 StatCache.get());
528 llvm::sys::Path FilePath(Path);
529 FixupRelativePath(FilePath, FileSystemOpts);
531 return FileSystemStatCache::get(FilePath.c_str(), StatBuf, FileDescriptor,
532 StatCache.get());
535 void FileManager::GetUniqueIDMapping(
536 llvm::SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
537 UIDToFiles.clear();
538 UIDToFiles.resize(NextFileUID);
540 // Map file entries
541 for (llvm::StringMap<FileEntry*, llvm::BumpPtrAllocator>::const_iterator
542 FE = SeenFileEntries.begin(), FEEnd = SeenFileEntries.end();
543 FE != FEEnd; ++FE)
544 if (FE->getValue() && FE->getValue() != NON_EXISTENT_FILE)
545 UIDToFiles[FE->getValue()->getUID()] = FE->getValue();
547 // Map virtual file entries
548 for (llvm::SmallVector<FileEntry*, 4>::const_iterator
549 VFE = VirtualFileEntries.begin(), VFEEnd = VirtualFileEntries.end();
550 VFE != VFEEnd; ++VFE)
551 if (*VFE && *VFE != NON_EXISTENT_FILE)
552 UIDToFiles[(*VFE)->getUID()] = *VFE;
556 void FileManager::PrintStats() const {
557 llvm::errs() << "\n*** File Manager Stats:\n";
558 llvm::errs() << UniqueRealFiles.size() << " real files found, "
559 << UniqueRealDirs.size() << " real dirs found.\n";
560 llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
561 << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
562 llvm::errs() << NumDirLookups << " dir lookups, "
563 << NumDirCacheMisses << " dir cache misses.\n";
564 llvm::errs() << NumFileLookups << " file lookups, "
565 << NumFileCacheMisses << " file cache misses.\n";
567 //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;