xref: /llvm-project/clang/lib/Basic/FileManager.cpp (revision 7a51313d8a0a358bb92eb5dbf8fd846b7c48e7fe)
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.
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"
22 #include "llvm/Bitcode/Serialize.h"
23 #include "llvm/Bitcode/Deserialize.h"
24 #include "llvm/Support/Streams.h"
25 #include "llvm/Config/config.h"
26 using namespace clang;
27 
28 // FIXME: Enhance libsystem to support inode and other fields.
29 #include <sys/stat.h>
30 
31 #if defined(_MSC_VER)
32 #define S_ISDIR(s) (_S_IFDIR & s)
33 #endif
34 
35 /// NON_EXISTENT_DIR - A special value distinct from null that is used to
36 /// represent a dir name that doesn't exist on the disk.
37 #define NON_EXISTENT_DIR reinterpret_cast<DirectoryEntry*>((intptr_t)-1)
38 
39 #ifdef LLVM_ON_WIN32
40 
41 #define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
42 
43 namespace {
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 
56 class FileManager::UniqueDirContainer {
57   /// UniqueDirs - Cache from full path to existing directories/files.
58   ///
59   llvm::StringMap<DirectoryEntry> UniqueDirs;
60 
61 public:
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 
73 class FileManager::UniqueFileContainer {
74   /// UniqueFiles - Cache from full path to existing directories/files.
75   ///
76   llvm::StringMap<FileEntry> UniqueFiles;
77 
78 public:
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 
94 class 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 
99 public:
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 
107 class FileManager::UniqueFileContainer {
108   /// UniqueFiles - Cache from ID's to existing directories/files.
109   ///
110   std::set<FileEntry> UniqueFiles;
111 
112 public:
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 
126 FileManager::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 
134 FileManager::~FileManager() {
135   delete &UniqueDirs;
136   delete &UniqueFiles;
137 }
138 
139 
140 /// getDirectory - Lookup, cache, and verify the specified directory.  This
141 /// returns null if the directory doesn't exist.
142 ///
143 const 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())
151     return NamedDirEnt.getValue() == NON_EXISTENT_DIR
152               ? 0 : NamedDirEnt.getValue();
153 
154   ++NumDirCacheMisses;
155 
156   // By default, initialize it to invalid.
157   NamedDirEnt.setValue(NON_EXISTENT_DIR);
158 
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;
168 
169   // It exists.  See if we have already opened a directory with the same inode.
170   // This occurs when one dir is symlinked to another, for example.
171   DirectoryEntry &UDE = UniqueDirs.getDirectory(InterndDirName, StatBuf);
172 
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 
183 /// NON_EXISTENT_FILE - A special value distinct from null that is used to
184 /// represent a filename that doesn't exist on the disk.
185 #define NON_EXISTENT_FILE reinterpret_cast<FileEntry*>((intptr_t)-1)
186 
187 /// getFile - Lookup, cache, and verify the specified file.  This returns null
188 /// if the file doesn't exist.
189 ///
190 const 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())
200     return NamedFileEnt.getValue() == NON_EXISTENT_FILE
201                  ? 0 : NamedFileEnt.getValue();
202 
203   ++NumFileCacheMisses;
204 
205   // By default, initialize it to invalid.
206   NamedFileEnt.setValue(NON_EXISTENT_FILE);
207 
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;
212   while (SlashPos >= NameStart && !IS_DIR_SEPARATOR_CHAR(SlashPos[0]))
213     --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;
237   //llvm::cerr << "STATING: " << Filename;
238   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.
241     //llvm::cerr << ": Not existing\n";
242     return 0;
243   }
244   //llvm::cerr << ": exists\n";
245 
246   // It exists.  See if we have already opened a file with the same inode.
247   // This occurs when one dir is symlinked to another, for example.
248   FileEntry &UFE = UniqueFiles.getFile(InterndFileName, StatBuf);
249 
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 
265 void FileManager::PrintStats() const {
266   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";
273 
274   //llvm::cerr << PagesMapped << BytesOfPagesMapped << FSLookups;
275 }
276