blob: 8ffde1f6fc94bf314c90b4a0dfb9711a3176407f [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Ted Kremenekf7260b12007-12-04 18:21:35 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000024#include <iostream>
Chris Lattner22eb9722006-06-18 05:43:12 +000025using namespace clang;
26
27// FIXME: Enhance libsystem to support inode and other fields.
28#include <sys/stat.h>
29
Chris Lattnerff3fa8b2007-09-03 18:37:14 +000030#if defined(_MSC_VER)
31#define S_ISDIR(s) (_S_IFDIR & s)
32#endif
Chris Lattneraf653752006-10-30 03:06:54 +000033
34/// NON_EXISTANT_DIR - A special value distinct from null that is used to
35/// represent a dir name that doesn't exist on the disk.
36#define NON_EXISTANT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
37
Chris Lattner22eb9722006-06-18 05:43:12 +000038/// getDirectory - Lookup, cache, and verify the specified directory. This
39/// returns null if the directory doesn't exist.
40///
Chris Lattner2f4a89a2006-10-30 03:55:17 +000041const DirectoryEntry *FileManager::getDirectory(const char *NameStart,
42 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +000043 ++NumDirLookups;
Chris Lattner23b7eb62007-06-15 23:05:46 +000044 llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +000045 DirEntries.GetOrCreateValue(NameStart, NameEnd);
Chris Lattneraf653752006-10-30 03:06:54 +000046
Chris Lattner22eb9722006-06-18 05:43:12 +000047 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000048 if (NamedDirEnt.getValue())
49 return NamedDirEnt.getValue() == NON_EXISTANT_DIR
50 ? 0 : NamedDirEnt.getValue();
Chris Lattner22eb9722006-06-18 05:43:12 +000051
52 ++NumDirCacheMisses;
53
Chris Lattneraf653752006-10-30 03:06:54 +000054 // By default, initialize it to invalid.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000055 NamedDirEnt.setValue(NON_EXISTANT_DIR);
Chris Lattner22eb9722006-06-18 05:43:12 +000056
Chris Lattner43fd42e2006-10-30 03:40:58 +000057 // Get the null-terminated directory name as stored as the key of the
58 // DirEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000059 const char *InterndDirName = NamedDirEnt.getKeyData();
Chris Lattner43fd42e2006-10-30 03:40:58 +000060
Chris Lattneraf653752006-10-30 03:06:54 +000061 // Check to see if the directory exists.
Chris Lattner22eb9722006-06-18 05:43:12 +000062 struct stat StatBuf;
Chris Lattner43fd42e2006-10-30 03:40:58 +000063 if (stat(InterndDirName, &StatBuf) || // Error stat'ing.
64 !S_ISDIR(StatBuf.st_mode)) // Not a directory?
Chris Lattner22eb9722006-06-18 05:43:12 +000065 return 0;
66
67 // It exists. See if we have already opened a directory with the same inode.
68 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +000069 DirectoryEntry &UDE =
Chris Lattner22eb9722006-06-18 05:43:12 +000070 UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
71
Chris Lattner34d1f5a2007-02-08 19:08:49 +000072 NamedDirEnt.setValue(&UDE);
73 if (UDE.getName()) // Already have an entry with this inode, return it.
74 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000075
Chris Lattnera85cbe22006-10-30 03:11:40 +000076 // Otherwise, we don't have this directory yet, add it. We use the string
77 // key from the DirEntries map as the string.
Chris Lattner43fd42e2006-10-30 03:40:58 +000078 UDE.Name = InterndDirName;
Chris Lattner34d1f5a2007-02-08 19:08:49 +000079 return &UDE;
Chris Lattner22eb9722006-06-18 05:43:12 +000080}
81
Chris Lattner2f4a89a2006-10-30 03:55:17 +000082/// NON_EXISTANT_FILE - A special value distinct from null that is used to
83/// represent a filename that doesn't exist on the disk.
84#define NON_EXISTANT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
85
Chris Lattner22eb9722006-06-18 05:43:12 +000086/// getFile - Lookup, cache, and verify the specified file. This returns null
87/// if the file doesn't exist.
88///
Chris Lattner2f4a89a2006-10-30 03:55:17 +000089const FileEntry *FileManager::getFile(const char *NameStart,
90 const char *NameEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +000091 ++NumFileLookups;
92
93 // See if there is already an entry in the map.
Chris Lattner23b7eb62007-06-15 23:05:46 +000094 llvm::StringMapEntry<FileEntry *> &NamedFileEnt =
Chris Lattner34d1f5a2007-02-08 19:08:49 +000095 FileEntries.GetOrCreateValue(NameStart, NameEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +000096
Chris Lattner2f4a89a2006-10-30 03:55:17 +000097 // See if there is already an entry in the map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +000098 if (NamedFileEnt.getValue())
99 return NamedFileEnt.getValue() == NON_EXISTANT_FILE
100 ? 0 : NamedFileEnt.getValue();
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000101
Chris Lattner22eb9722006-06-18 05:43:12 +0000102 ++NumFileCacheMisses;
103
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000104 // By default, initialize it to invalid.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000105 NamedFileEnt.setValue(NON_EXISTANT_FILE);
Chris Lattner22eb9722006-06-18 05:43:12 +0000106
Chris Lattner9c59bda2006-10-30 04:34:28 +0000107 // Figure out what directory it is in. If the string contains a / in it,
108 // strip off everything after it.
Chris Lattner22eb9722006-06-18 05:43:12 +0000109 // FIXME: this logic should be in sys::Path.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000110 const char *SlashPos = NameEnd-1;
111 while (SlashPos >= NameStart && SlashPos[0] != '/')
112 --SlashPos;
113
Chris Lattner9c59bda2006-10-30 04:34:28 +0000114 const DirectoryEntry *DirInfo;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000115 if (SlashPos < NameStart) {
116 // Use the current directory if file has no path component.
Chris Lattner9c59bda2006-10-30 04:34:28 +0000117 const char *Name = ".";
118 DirInfo = getDirectory(Name, Name+1);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000119 } else if (SlashPos == NameEnd-1)
Chris Lattner22eb9722006-06-18 05:43:12 +0000120 return 0; // If filename ends with a /, it's a directory.
121 else
Chris Lattner9c59bda2006-10-30 04:34:28 +0000122 DirInfo = getDirectory(NameStart, SlashPos);
123
Chris Lattner22eb9722006-06-18 05:43:12 +0000124 if (DirInfo == 0) // Directory doesn't exist, file can't exist.
125 return 0;
126
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000127 // Get the null-terminated file name as stored as the key of the
128 // FileEntries map.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000129 const char *InterndFileName = NamedFileEnt.getKeyData();
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000130
Chris Lattner22eb9722006-06-18 05:43:12 +0000131 // FIXME: Use the directory info to prune this, before doing the stat syscall.
132 // FIXME: This will reduce the # syscalls.
133
134 // Nope, there isn't. Check to see if the file exists.
135 struct stat StatBuf;
Chris Lattner81500bc2006-07-19 03:40:07 +0000136 //std::cerr << "STATING: " << Filename;
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000137 if (stat(InterndFileName, &StatBuf) || // Error stat'ing.
Chris Lattner81500bc2006-07-19 03:40:07 +0000138 S_ISDIR(StatBuf.st_mode)) { // A directory?
139 // If this file doesn't exist, we leave a null in FileEntries for this path.
140 //std::cerr << ": Not existing\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000141 return 0;
Chris Lattner81500bc2006-07-19 03:40:07 +0000142 }
143 //std::cerr << ": exists\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000144
145 // It exists. See if we have already opened a directory with the same inode.
146 // This occurs when one dir is symlinked to another, for example.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000147 FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
Chris Lattner22eb9722006-06-18 05:43:12 +0000148
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000149 NamedFileEnt.setValue(&UFE);
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000150 if (UFE.getName()) // Already have an entry with this inode, return it.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000151 return &UFE;
Chris Lattner269c2322006-06-25 06:23:00 +0000152
Chris Lattner22eb9722006-06-18 05:43:12 +0000153 // Otherwise, we don't have this directory yet, add it.
Chris Lattner8b1e8482006-10-30 02:45:16 +0000154 // FIXME: Change the name to be a char* that points back to the 'FileEntries'
155 // key.
Chris Lattner2f4a89a2006-10-30 03:55:17 +0000156 UFE.Name = InterndFileName;
157 UFE.Size = StatBuf.st_size;
158 UFE.ModTime = StatBuf.st_mtime;
159 UFE.Dir = DirInfo;
160 UFE.UID = NextFileUID++;
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000161 return &UFE;
Chris Lattner22eb9722006-06-18 05:43:12 +0000162}
163
164void FileManager::PrintStats() const {
165 std::cerr << "\n*** File Manager Stats:\n";
166 std::cerr << UniqueFiles.size() << " files found, "
167 << UniqueDirs.size() << " dirs found.\n";
168 std::cerr << NumDirLookups << " dir lookups, "
169 << NumDirCacheMisses << " dir cache misses.\n";
170 std::cerr << NumFileLookups << " file lookups, "
171 << NumFileCacheMisses << " file cache misses.\n";
172
173 //std::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
174}