Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/FileManager.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- FileManager.cpp - File System Probing and Caching ----------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file implements the FileManager interface.
10
//
11
//===----------------------------------------------------------------------===//
12
//
13
// TODO: This should index all interesting directories with dirent calls.
14
//  getdirentries ?
15
//  opendir/readdir_r/closedir ?
16
//
17
//===----------------------------------------------------------------------===//
18
19
#include "clang/Basic/FileManager.h"
20
#include "clang/Basic/FileSystemStatCache.h"
21
#include "llvm/ADT/STLExtras.h"
22
#include "llvm/ADT/SmallString.h"
23
#include "llvm/ADT/Statistic.h"
24
#include "llvm/Config/llvm-config.h"
25
#include "llvm/Support/FileSystem.h"
26
#include "llvm/Support/MemoryBuffer.h"
27
#include "llvm/Support/Path.h"
28
#include "llvm/Support/raw_ostream.h"
29
#include <algorithm>
30
#include <cassert>
31
#include <climits>
32
#include <cstdint>
33
#include <cstdlib>
34
#include <string>
35
#include <utility>
36
37
using namespace clang;
38
39
#define DEBUG_TYPE "file-search"
40
41
ALWAYS_ENABLED_STATISTIC(NumDirLookups, "Number of directory lookups.");
42
ALWAYS_ENABLED_STATISTIC(NumFileLookups, "Number of file lookups.");
43
ALWAYS_ENABLED_STATISTIC(NumDirCacheMisses,
44
                         "Number of directory cache misses.");
45
ALWAYS_ENABLED_STATISTIC(NumFileCacheMisses, "Number of file cache misses.");
46
47
//===----------------------------------------------------------------------===//
48
// Common logic.
49
//===----------------------------------------------------------------------===//
50
51
FileManager::FileManager(const FileSystemOptions &FSO,
52
                         IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
53
    : FS(std::move(FS)), FileSystemOpts(FSO), SeenDirEntries(64),
54
174k
      SeenFileEntries(64), NextFileUID(0) {
55
  // If the caller doesn't provide a virtual file system, just grab the real
56
  // file system.
57
174k
  if (!this->FS)
58
287
    this->FS = llvm::vfs::getRealFileSystem();
59
174k
}
60
61
166k
FileManager::~FileManager() = default;
62
63
12
void FileManager::setStatCache(std::unique_ptr<FileSystemStatCache> statCache) {
64
12
  assert(statCache && "No stat cache provided?");
65
12
  StatCache = std::move(statCache);
66
12
}
67
68
16.1k
void FileManager::clearStatCache() { StatCache.reset(); }
69
70
/// Retrieve the directory that the given file name resides in.
71
/// Filename can point to either a real file or a virtual file.
72
static llvm::ErrorOr<const DirectoryEntry *>
73
getDirectoryFromFile(FileManager &FileMgr, StringRef Filename,
74
5.92M
                     bool CacheFailure) {
75
5.92M
  if (Filename.empty())
76
0
    return std::errc::no_such_file_or_directory;
77
5.92M
78
5.92M
  if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
79
20
    return std::errc::is_a_directory;
80
5.92M
81
5.92M
  StringRef DirName = llvm::sys::path::parent_path(Filename);
82
  // Use the current directory if file has no path component.
83
5.92M
  if (DirName.empty())
84
76.6k
    DirName = ".";
85
5.92M
86
5.92M
  return FileMgr.getDirectory(DirName, CacheFailure);
87
5.92M
}
88
89
/// Add all ancestors of the given path (pointing to either a file or
90
/// a directory) as virtual directories.
91
13.4k
void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
92
13.4k
  StringRef DirName = llvm::sys::path::parent_path(Path);
93
13.4k
  if (DirName.empty())
94
6.67k
    DirName = ".";
95
13.4k
96
13.4k
  auto &NamedDirEnt = *SeenDirEntries.insert(
97
13.4k
        {DirName, std::errc::no_such_file_or_directory}).first;
98
13.4k
99
  // When caching a virtual directory, we always cache its ancestors
100
  // at the same time.  Therefore, if DirName is already in the cache,
101
  // we don't need to recurse as its ancestors must also already be in
102
  // the cache (or it's a known non-virtual directory).
103
13.4k
  if (NamedDirEnt.second)
104
4.91k
    return;
105
8.49k
106
  // Add the virtual directory to the cache.
107
8.49k
  auto UDE = std::make_unique<DirectoryEntry>();
108
8.49k
  UDE->Name = NamedDirEnt.first();
109
8.49k
  NamedDirEnt.second = *UDE.get();
110
8.49k
  VirtualDirectoryEntries.push_back(std::move(UDE));
111
8.49k
112
  // Recursively add the other ancestors.
113
8.49k
  addAncestorsAsVirtualDirs(DirName);
114
8.49k
}
115
116
llvm::Expected<DirectoryEntryRef>
117
21.3M
FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
118
  // stat doesn't like trailing separators except for root directory.
119
  // At least, on Win32 MSVCRT, stat() cannot strip trailing '/'.
120
  // (though it can strip '\\')
121
21.3M
  if (DirName.size() > 1 &&
122
20.4M
      DirName != llvm::sys::path::root_path(DirName) &&
123
20.4M
      llvm::sys::path::is_separator(DirName.back()))
124
29.6k
    DirName = DirName.substr(0, DirName.size()-1);
125
#ifdef _WIN32
126
  // Fixing a problem with "clang C:test.c" on Windows.
127
  // Stat("C:") does not recognize "C:" as a valid directory
128
  std::string DirNameStr;
129
  if (DirName.size() > 1 && DirName.back() == ':' &&
130
      DirName.equals_lower(llvm::sys::path::root_name(DirName))) {
131
    DirNameStr = DirName.str() + '.';
132
    DirName = DirNameStr;
133
  }
134
#endif
135
21.3M
136
21.3M
  ++NumDirLookups;
137
21.3M
138
  // See if there was already an entry in the map.  Note that the map
139
  // contains both virtual and real directories.
140
21.3M
  auto SeenDirInsertResult =
141
21.3M
      SeenDirEntries.insert({DirName, std::errc::no_such_file_or_directory});
142
21.3M
  if (!SeenDirInsertResult.second) {
143
20.0M
    if (SeenDirInsertResult.first->second)
144
17.2M
      return DirectoryEntryRef(&*SeenDirInsertResult.first);
145
2.76M
    return llvm::errorCodeToError(SeenDirInsertResult.first->second.getError());
146
2.76M
  }
147
1.36M
148
  // We've not seen this before. Fill it in.
149
1.36M
  ++NumDirCacheMisses;
150
1.36M
  auto &NamedDirEnt = *SeenDirInsertResult.first;
151
1.36M
  assert(!NamedDirEnt.second && "should be newly-created");
152
1.36M
153
  // Get the null-terminated directory name as stored as the key of the
154
  // SeenDirEntries map.
155
1.36M
  StringRef InterndDirName = NamedDirEnt.first();
156
1.36M
157
  // Check to see if the directory exists.
158
1.36M
  llvm::vfs::Status Status;
159
1.36M
  auto statError = getStatValue(InterndDirName, Status, false,
160
1.36M
                                nullptr /*directory lookup*/);
161
1.36M
  if (statError) {
162
    // There's no real directory at the given path.
163
867k
    if (CacheFailure)
164
866k
      NamedDirEnt.second = statError;
165
636
    else
166
636
      SeenDirEntries.erase(DirName);
167
867k
    return llvm::errorCodeToError(statError);
168
867k
  }
169
493k
170
  // It exists.  See if we have already opened a directory with the
171
  // same inode (this occurs on Unix-like systems when one dir is
172
  // symlinked to another, for example) or the same path (on
173
  // Windows).
174
493k
  DirectoryEntry &UDE = UniqueRealDirs[Status.getUniqueID()];
175
493k
176
493k
  NamedDirEnt.second = UDE;
177
493k
  if (UDE.getName().empty()) {
178
    // We don't have this directory yet, add it.  We use the string
179
    // key from the SeenDirEntries map as the string.
180
487k
    UDE.Name  = InterndDirName;
181
487k
  }
182
493k
183
493k
  return DirectoryEntryRef(&NamedDirEnt);
184
493k
}
185
186
llvm::ErrorOr<const DirectoryEntry *>
187
21.1M
FileManager::getDirectory(StringRef DirName, bool CacheFailure) {
188
21.1M
  auto Result = getDirectoryRef(DirName, CacheFailure);
189
21.1M
  if (Result)
190
17.6M
    return &Result->getDirEntry();
191
3.53M
  return llvm::errorToErrorCode(Result.takeError());
192
3.53M
}
193
194
llvm::ErrorOr<const FileEntry *>
195
3.33M
FileManager::getFile(StringRef Filename, bool openFile, bool CacheFailure) {
196
3.33M
  auto Result = getFileRef(Filename, openFile, CacheFailure);
197
3.33M
  if (Result)
198
3.10M
    return &Result->getFileEntry();
199
228k
  return llvm::errorToErrorCode(Result.takeError());
200
228k
}
201
202
llvm::Expected<FileEntryRef>
203
9.12M
FileManager::getFileRef(StringRef Filename, bool openFile, bool CacheFailure) {
204
9.12M
  ++NumFileLookups;
205
9.12M
206
  // See if there is already an entry in the map.
207
9.12M
  auto SeenFileInsertResult =
208
9.12M
      SeenFileEntries.insert({Filename, std::errc::no_such_file_or_directory});
209
9.12M
  if (!SeenFileInsertResult.second) {
210
3.20M
    if (!SeenFileInsertResult.first->second)
211
88.7k
      return llvm::errorCodeToError(
212
88.7k
          SeenFileInsertResult.first->second.getError());
213
    // Construct and return and FileEntryRef, unless it's a redirect to another
214
    // filename.
215
3.11M
    SeenFileEntryOrRedirect Value = *SeenFileInsertResult.first->second;
216
3.11M
    FileEntry *FE;
217
3.11M
    if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>()))
218
3.11M
      return FileEntryRef(SeenFileInsertResult.first->first(), *FE);
219
461
    return getFileRef(*Value.get<const StringRef *>(), openFile, CacheFailure);
220
461
  }
221
5.92M
222
  // We've not seen this before. Fill it in.
223
5.92M
  ++NumFileCacheMisses;
224
5.92M
  auto *NamedFileEnt = &*SeenFileInsertResult.first;
225
5.92M
  assert(!NamedFileEnt->second && "should be newly-created");
226
5.92M
227
  // Get the null-terminated file name as stored as the key of the
228
  // SeenFileEntries map.
229
5.92M
  StringRef InterndFileName = NamedFileEnt->first();
230
5.92M
231
  // Look up the directory for the file.  When looking up something like
232
  // sys/foo.h we'll discover all of the search directories that have a 'sys'
233
  // subdirectory.  This will let us avoid having to waste time on known-to-fail
234
  // searches when we go to find sys/bar.h, because all the search directories
235
  // without a 'sys' subdir will get a cached failure result.
236
5.92M
  auto DirInfoOrErr = getDirectoryFromFile(*this, Filename, CacheFailure);
237
5.92M
  if (!DirInfoOrErr) { // Directory doesn't exist, file can't exist.
238
2.99M
    if (CacheFailure)
239
2.99M
      NamedFileEnt->second = DirInfoOrErr.getError();
240
636
    else
241
636
      SeenFileEntries.erase(Filename);
242
2.99M
243
2.99M
    return llvm::errorCodeToError(DirInfoOrErr.getError());
244
2.99M
  }
245
2.92M
  const DirectoryEntry *DirInfo = *DirInfoOrErr;
246
2.92M
247
  // FIXME: Use the directory info to prune this, before doing the stat syscall.
248
  // FIXME: This will reduce the # syscalls.
249
2.92M
250
  // Check to see if the file exists.
251
2.92M
  std::unique_ptr<llvm::vfs::File> F;
252
2.92M
  llvm::vfs::Status Status;
253
2.92M
  auto statError = getStatValue(InterndFileName, Status, true,
254
1.90M
                                openFile ? 
&F1.02M
: nullptr);
255
2.92M
  if (statError) {
256
    // There's no real file at the given path.
257
525k
    if (CacheFailure)
258
524k
      NamedFileEnt->second = statError;
259
903
    else
260
903
      SeenFileEntries.erase(Filename);
261
525k
262
525k
    return llvm::errorCodeToError(statError);
263
525k
  }
264
2.40M
265
2.40M
  assert((openFile || !F) && "undesired open file");
266
2.40M
267
  // It exists.  See if we have already opened a file with the same inode.
268
  // This occurs when one dir is symlinked to another, for example.
269
2.40M
  FileEntry &UFE = UniqueRealFiles[Status.getUniqueID()];
270
2.40M
271
2.40M
  NamedFileEnt->second = &UFE;
272
2.40M
273
  // If the name returned by getStatValue is different than Filename, re-intern
274
  // the name.
275
2.40M
  if (Status.getName() != Filename) {
276
173
    auto &NewNamedFileEnt =
277
173
        *SeenFileEntries.insert({Status.getName(), &UFE}).first;
278
173
    assert((*NewNamedFileEnt.second).get<FileEntry *>() == &UFE &&
279
173
           "filename from getStatValue() refers to wrong file");
280
173
    InterndFileName = NewNamedFileEnt.first().data();
281
    // In addition to re-interning the name, construct a redirecting seen file
282
    // entry, that will point to the name the filesystem actually wants to use.
283
173
    StringRef *Redirect = new (CanonicalNameStorage) StringRef(InterndFileName);
284
173
    auto SeenFileInsertResultIt = SeenFileEntries.find(Filename);
285
173
    assert(SeenFileInsertResultIt != SeenFileEntries.end() &&
286
173
           "unexpected SeenFileEntries cache miss");
287
173
    SeenFileInsertResultIt->second = Redirect;
288
173
    NamedFileEnt = &*SeenFileInsertResultIt;
289
173
  }
290
2.40M
291
2.40M
  if (UFE.isValid()) { // Already have an entry with this inode, return it.
292
8.65k
293
    // FIXME: this hack ensures that if we look up a file by a virtual path in
294
    // the VFS that the getDir() will have the virtual path, even if we found
295
    // the file by a 'real' path first. This is required in order to find a
296
    // module's structure when its headers/module map are mapped in the VFS.
297
    // We should remove this as soon as we can properly support a file having
298
    // multiple names.
299
8.65k
    if (DirInfo != UFE.Dir && 
Status.IsVFSMapped5.06k
)
300
12
      UFE.Dir = DirInfo;
301
8.65k
302
    // Always update the name to use the last name by which a file was accessed.
303
    // FIXME: Neither this nor always using the first name is correct; we want
304
    // to switch towards a design where we return a FileName object that
305
    // encapsulates both the name by which the file was accessed and the
306
    // corresponding FileEntry.
307
    // FIXME: The Name should be removed from FileEntry once all clients
308
    // adopt FileEntryRef.
309
8.65k
    UFE.Name = InterndFileName;
310
8.65k
311
8.65k
    return FileEntryRef(InterndFileName, UFE);
312
8.65k
  }
313
2.39M
314
  // Otherwise, we don't have this file yet, add it.
315
2.39M
  UFE.Name    = InterndFileName;
316
2.39M
  UFE.Size    = Status.getSize();
317
2.39M
  UFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
318
2.39M
  UFE.Dir     = DirInfo;
319
2.39M
  UFE.UID     = NextFileUID++;
320
2.39M
  UFE.UniqueID = Status.getUniqueID();
321
2.39M
  UFE.IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
322
2.39M
  UFE.File = std::move(F);
323
2.39M
  UFE.IsValid = true;
324
2.39M
325
2.39M
  if (UFE.File) {
326
611k
    if (auto PathName = UFE.File->getName())
327
611k
      fillRealPathName(&UFE, *PathName);
328
1.78M
  } else 
if (1.78M
!openFile1.78M
) {
329
    // We should still fill the path even if we aren't opening the file.
330
1.78M
    fillRealPathName(&UFE, InterndFileName);
331
1.78M
  }
332
2.39M
  return FileEntryRef(InterndFileName, UFE);
333
2.39M
}
334
335
const FileEntry *
336
FileManager::getVirtualFile(StringRef Filename, off_t Size,
337
5.08k
                            time_t ModificationTime) {
338
5.08k
  ++NumFileLookups;
339
5.08k
340
  // See if there is already an entry in the map for an existing file.
341
5.08k
  auto &NamedFileEnt = *SeenFileEntries.insert(
342
5.08k
      {Filename, std::errc::no_such_file_or_directory}).first;
343
5.08k
  if (NamedFileEnt.second) {
344
168
    SeenFileEntryOrRedirect Value = *NamedFileEnt.second;
345
168
    FileEntry *FE;
346
168
    if (LLVM_LIKELY(FE = Value.dyn_cast<FileEntry *>()))
347
168
      return FE;
348
0
    return getVirtualFile(*Value.get<const StringRef *>(), Size,
349
0
                          ModificationTime);
350
0
  }
351
4.91k
352
  // We've not seen this before, or the file is cached as non-existent.
353
4.91k
  ++NumFileCacheMisses;
354
4.91k
  addAncestorsAsVirtualDirs(Filename);
355
4.91k
  FileEntry *UFE = nullptr;
356
4.91k
357
  // Now that all ancestors of Filename are in the cache, the
358
  // following call is guaranteed to find the DirectoryEntry from the
359
  // cache.
360
4.91k
  auto DirInfo = getDirectoryFromFile(*this, Filename, /*CacheFailure=*/true);
361
4.91k
  assert(DirInfo &&
362
4.91k
         "The directory of a virtual file should already be in the cache.");
363
4.91k
364
  // Check to see if the file exists. If so, drop the virtual file
365
4.91k
  llvm::vfs::Status Status;
366
4.91k
  const char *InterndFileName = NamedFileEnt.first().data();
367
4.91k
  if (!getStatValue(InterndFileName, Status, true, nullptr)) {
368
2.34k
    UFE = &UniqueRealFiles[Status.getUniqueID()];
369
2.34k
    Status = llvm::vfs::Status(
370
2.34k
      Status.getName(), Status.getUniqueID(),
371
2.34k
      llvm::sys::toTimePoint(ModificationTime),
372
2.34k
      Status.getUser(), Status.getGroup(), Size,
373
2.34k
      Status.getType(), Status.getPermissions());
374
2.34k
375
2.34k
    NamedFileEnt.second = UFE;
376
2.34k
377
    // If we had already opened this file, close it now so we don't
378
    // leak the descriptor. We're not going to use the file
379
    // descriptor anyway, since this is a virtual file.
380
2.34k
    if (UFE->File)
381
0
      UFE->closeFile();
382
2.34k
383
    // If we already have an entry with this inode, return it.
384
2.34k
    if (UFE->isValid())
385
1
      return UFE;
386
2.34k
387
2.34k
    UFE->UniqueID = Status.getUniqueID();
388
2.34k
    UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
389
2.34k
    fillRealPathName(UFE, Status.getName());
390
2.57k
  } else {
391
2.57k
    VirtualFileEntries.push_back(std::make_unique<FileEntry>());
392
2.57k
    UFE = VirtualFileEntries.back().get();
393
2.57k
    NamedFileEnt.second = UFE;
394
2.57k
  }
395
4.91k
396
4.91k
  UFE->Name    = InterndFileName;
397
4.91k
  UFE->Size    = Size;
398
4.91k
  UFE->ModTime = ModificationTime;
399
4.91k
  UFE->Dir     = *DirInfo;
400
4.91k
  UFE->UID     = NextFileUID++;
401
4.91k
  UFE->IsValid = true;
402
4.91k
  UFE->File.reset();
403
4.91k
  return UFE;
404
4.91k
}
405
406
2
llvm::Optional<FileEntryRef> FileManager::getBypassFile(FileEntryRef VF) {
407
  // Stat of the file and return nullptr if it doesn't exist.
408
2
  llvm::vfs::Status Status;
409
2
  if (getStatValue(VF.getName(), Status, /*isFile=*/true, /*F=*/nullptr))
410
0
    return None;
411
2
412
  // Fill it in from the stat.
413
2
  BypassFileEntries.push_back(std::make_unique<FileEntry>());
414
2
  const FileEntry &VFE = VF.getFileEntry();
415
2
  FileEntry &BFE = *BypassFileEntries.back();
416
2
  BFE.Name = VFE.getName();
417
2
  BFE.Size = Status.getSize();
418
2
  BFE.Dir = VFE.Dir;
419
2
  BFE.ModTime = llvm::sys::toTimeT(Status.getLastModificationTime());
420
2
  BFE.UID = NextFileUID++;
421
2
  BFE.IsValid = true;
422
2
  return FileEntryRef(VF.getName(), BFE);
423
2
}
424
425
2.51M
bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
426
2.51M
  StringRef pathRef(path.data(), path.size());
427
2.51M
428
2.51M
  if (FileSystemOpts.WorkingDir.empty()
429
1.37k
      || llvm::sys::path::is_absolute(pathRef))
430
2.51M
    return false;
431
313
432
313
  SmallString<128> NewPath(FileSystemOpts.WorkingDir);
433
313
  llvm::sys::path::append(NewPath, pathRef);
434
313
  path = NewPath;
435
313
  return true;
436
313
}
437
438
2.48M
bool FileManager::makeAbsolutePath(SmallVectorImpl<char> &Path) const {
439
2.48M
  bool Changed = FixupRelativePath(Path);
440
2.48M
441
2.48M
  if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
442
89.1k
    FS->makeAbsolute(Path);
443
89.1k
    Changed = true;
444
89.1k
  }
445
2.48M
446
2.48M
  return Changed;
447
2.48M
}
448
449
2.39M
void FileManager::fillRealPathName(FileEntry *UFE, llvm::StringRef FileName) {
450
2.39M
  llvm::SmallString<128> AbsPath(FileName);
451
  // This is not the same as `VFS::getRealPath()`, which resolves symlinks
452
  // but can be very expensive on real file systems.
453
  // FIXME: the semantic of RealPathName is unclear, and the name might be
454
  // misleading. We need to clean up the interface here.
455
2.39M
  makeAbsolutePath(AbsPath);
456
2.39M
  llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
457
2.39M
  UFE->RealPathName = std::string(AbsPath.str());
458
2.39M
}
459
460
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
461
FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
462
976k
                              bool RequiresNullTerminator) {
463
976k
  uint64_t FileSize = Entry->getSize();
464
  // If there's a high enough chance that the file have changed since we
465
  // got its size, force a stat before opening it.
466
976k
  if (isVolatile)
467
22.0k
    FileSize = -1;
468
976k
469
976k
  StringRef Filename = Entry->getName();
470
  // If the file is already open, use the open file descriptor.
471
976k
  if (Entry->File) {
472
607k
    auto Result = Entry->File->getBuffer(Filename, FileSize,
473
607k
                                         RequiresNullTerminator, isVolatile);
474
607k
    Entry->closeFile();
475
607k
    return Result;
476
607k
  }
477
368k
478
  // Otherwise, open the file.
479
368k
  return getBufferForFileImpl(Filename, FileSize, isVolatile,
480
368k
                              RequiresNullTerminator);
481
368k
}
482
483
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
484
FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
485
                                  bool isVolatile,
486
372k
                                  bool RequiresNullTerminator) {
487
372k
  if (FileSystemOpts.WorkingDir.empty())
488
372k
    return FS->getBufferForFile(Filename, FileSize, RequiresNullTerminator,
489
372k
                                isVolatile);
490
116
491
116
  SmallString<128> FilePath(Filename);
492
116
  FixupRelativePath(FilePath);
493
116
  return FS->getBufferForFile(FilePath, FileSize, RequiresNullTerminator,
494
116
                              isVolatile);
495
116
}
496
497
/// getStatValue - Get the 'stat' information for the specified path,
498
/// using the cache to accelerate it if possible.  This returns true
499
/// if the path points to a virtual file or does not exist, or returns
500
/// false if it's an existent real file.  If FileDescriptor is NULL,
501
/// do directory look-up instead of file look-up.
502
std::error_code
503
FileManager::getStatValue(StringRef Path, llvm::vfs::Status &Status,
504
4.29M
                          bool isFile, std::unique_ptr<llvm::vfs::File> *F) {
505
  // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
506
  // absolute!
507
4.29M
  if (FileSystemOpts.WorkingDir.empty())
508
4.29M
    return FileSystemStatCache::get(Path, Status, isFile, F,
509
4.29M
                                    StatCache.get(), *FS);
510
846
511
846
  SmallString<128> FilePath(Path);
512
846
  FixupRelativePath(FilePath);
513
846
514
846
  return FileSystemStatCache::get(FilePath.c_str(), Status, isFile, F,
515
846
                                  StatCache.get(), *FS);
516
846
}
517
518
std::error_code
519
FileManager::getNoncachedStatValue(StringRef Path,
520
16.9k
                                   llvm::vfs::Status &Result) {
521
16.9k
  SmallString<128> FilePath(Path);
522
16.9k
  FixupRelativePath(FilePath);
523
16.9k
524
16.9k
  llvm::ErrorOr<llvm::vfs::Status> S = FS->status(FilePath.c_str());
525
16.9k
  if (!S)
526
16.8k
    return S.getError();
527
9
  Result = *S;
528
9
  return std::error_code();
529
9
}
530
531
void FileManager::GetUniqueIDMapping(
532
5.04k
                   SmallVectorImpl<const FileEntry *> &UIDToFiles) const {
533
5.04k
  UIDToFiles.clear();
534
5.04k
  UIDToFiles.resize(NextFileUID);
535
5.04k
536
  // Map file entries
537
5.04k
  for (llvm::StringMap<llvm::ErrorOr<SeenFileEntryOrRedirect>,
538
5.04k
                       llvm::BumpPtrAllocator>::const_iterator
539
5.04k
           FE = SeenFileEntries.begin(),
540
5.04k
           FEEnd = SeenFileEntries.end();
541
896k
       FE != FEEnd; 
++FE891k
)
542
891k
    if (llvm::ErrorOr<SeenFileEntryOrRedirect> Entry = FE->getValue()) {
543
350k
      if (const auto *FE = (*Entry).dyn_cast<FileEntry *>())
544
349k
        UIDToFiles[FE->getUID()] = FE;
545
350k
    }
546
5.04k
547
  // Map virtual file entries
548
5.04k
  for (const auto &VFE : VirtualFileEntries)
549
450
    UIDToFiles[VFE->getUID()] = VFE.get();
550
5.04k
}
551
552
910k
StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
553
910k
  llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
554
910k
    = CanonicalNames.find(Dir);
555
910k
  if (Known != CanonicalNames.end())
556
884k
    return Known->second;
557
25.9k
558
25.9k
  StringRef CanonicalName(Dir->getName());
559
25.9k
560
25.9k
  SmallString<4096> CanonicalNameBuf;
561
25.9k
  if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
562
25.9k
    CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
563
25.9k
564
25.9k
  CanonicalNames.insert({Dir, CanonicalName});
565
25.9k
  return CanonicalName;
566
25.9k
}
567
568
3
StringRef FileManager::getCanonicalName(const FileEntry *File) {
569
3
  llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
570
3
    = CanonicalNames.find(File);
571
3
  if (Known != CanonicalNames.end())
572
1
    return Known->second;
573
2
574
2
  StringRef CanonicalName(File->getName());
575
2
576
2
  SmallString<4096> CanonicalNameBuf;
577
2
  if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
578
2
    CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
579
2
580
2
  CanonicalNames.insert({File, CanonicalName});
581
2
  return CanonicalName;
582
2
}
583
584
4
void FileManager::PrintStats() const {
585
4
  llvm::errs() << "\n*** File Manager Stats:\n";
586
4
  llvm::errs() << UniqueRealFiles.size() << " real files found, "
587
4
               << UniqueRealDirs.size() << " real dirs found.\n";
588
4
  llvm::errs() << VirtualFileEntries.size() << " virtual files found, "
589
4
               << VirtualDirectoryEntries.size() << " virtual dirs found.\n";
590
4
  llvm::errs() << NumDirLookups << " dir lookups, "
591
4
               << NumDirCacheMisses << " dir cache misses.\n";
592
4
  llvm::errs() << NumFileLookups << " file lookups, "
593
4
               << NumFileCacheMisses << " file cache misses.\n";
594
4
595
  //llvm::errs() << PagesMapped << BytesOfPagesMapped << FSLookups;
596
4
}