Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Basic/FileSystemStatCache.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- FileSystemStatCache.cpp - Caching for 'stat' calls -----------------===//
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 defines the FileSystemStatCache interface.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/FileSystemStatCache.h"
14
#include "llvm/Support/Chrono.h"
15
#include "llvm/Support/ErrorOr.h"
16
#include "llvm/Support/Path.h"
17
#include "llvm/Support/VirtualFileSystem.h"
18
#include <utility>
19
20
using namespace clang;
21
22
0
void FileSystemStatCache::anchor() {}
23
24
/// FileSystemStatCache::get - Get the 'stat' information for the specified
25
/// path, using the cache to accelerate it if possible.  This returns true if
26
/// the path does not exist or false if it exists.
27
///
28
/// If isFile is true, then this lookup should only return success for files
29
/// (not directories).  If it is false this lookup should only return
30
/// success for directories (not files).  On a successful file lookup, the
31
/// implementation can optionally fill in FileDescriptor with a valid
32
/// descriptor and the client guarantees that it will close it.
33
std::error_code
34
FileSystemStatCache::get(StringRef Path, llvm::vfs::Status &Status,
35
                         bool isFile, std::unique_ptr<llvm::vfs::File> *F,
36
                         FileSystemStatCache *Cache,
37
1.17M
                         llvm::vfs::FileSystem &FS) {
38
1.17M
  bool isForDir = !isFile;
39
1.17M
  std::error_code RetCode;
40
1.17M
41
1.17M
  // If we have a cache, use it to resolve the stat query.
42
1.17M
  if (Cache)
43
24
    RetCode = Cache->getStat(Path, Status, isFile, F, FS);
44
1.17M
  else if (isForDir || 
!F773k
) {
45
530k
    // If this is a directory or a file descriptor is not needed and we have
46
530k
    // no cache, just go to the file system.
47
530k
    llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = FS.status(Path);
48
530k
    if (!StatusOrErr) {
49
184k
      RetCode = StatusOrErr.getError();
50
345k
    } else {
51
345k
      Status = *StatusOrErr;
52
345k
    }
53
649k
  } else {
54
649k
    // Otherwise, we have to go to the filesystem.  We can always just use
55
649k
    // 'stat' here, but (for files) the client is asking whether the file exists
56
649k
    // because it wants to turn around and *open* it.  It is more efficient to
57
649k
    // do "open+fstat" on success than it is to do "stat+open".
58
649k
    //
59
649k
    // Because of this, check to see if the file exists with 'open'.  If the
60
649k
    // open succeeds, use fstat to get the stat info.
61
649k
    auto OwnedFile = FS.openFileForRead(Path);
62
649k
63
649k
    if (!OwnedFile) {
64
142k
      // If the open fails, our "stat" fails.
65
142k
      RetCode = OwnedFile.getError();
66
506k
    } else {
67
506k
      // Otherwise, the open succeeded.  Do an fstat to get the information
68
506k
      // about the file.  We'll end up returning the open file descriptor to the
69
506k
      // client to do what they please with it.
70
506k
      llvm::ErrorOr<llvm::vfs::Status> StatusOrErr = (*OwnedFile)->status();
71
506k
      if (
StatusOrErr506k
) {
72
506k
        Status = *StatusOrErr;
73
506k
        *F = std::move(*OwnedFile);
74
18.4E
      } else {
75
18.4E
        // fstat rarely fails.  If it does, claim the initial open didn't
76
18.4E
        // succeed.
77
18.4E
        *F = nullptr;
78
18.4E
        RetCode = StatusOrErr.getError();
79
18.4E
      }
80
506k
    }
81
649k
  }
82
1.17M
83
1.17M
  // If the path doesn't exist, return failure.
84
1.17M
  if (RetCode)
85
327k
    return RetCode;
86
851k
87
851k
  // If the path exists, make sure that its "directoryness" matches the clients
88
851k
  // demands.
89
851k
  if (Status.isDirectory() != isForDir) {
90
5.52k
    // If not, close the file if opened.
91
5.52k
    if (F)
92
1
      *F = nullptr;
93
5.52k
    return std::make_error_code(
94
5.52k
        Status.isDirectory() ?
95
5.51k
            
std::errc::is_a_directory15
: std::errc::not_a_directory);
96
5.52k
  }
97
846k
98
846k
  return std::error_code();
99
846k
}
100
101
std::error_code
102
MemorizeStatCalls::getStat(StringRef Path, llvm::vfs::Status &Status,
103
                           bool isFile,
104
                           std::unique_ptr<llvm::vfs::File> *F,
105
0
                           llvm::vfs::FileSystem &FS) {
106
0
  auto err = get(Path, Status, isFile, F, nullptr, FS);
107
0
  if (err) {
108
0
    // Do not cache failed stats, it is easy to construct common inconsistent
109
0
    // situations if we do, and they are not important for PCH performance
110
0
    // (which currently only needs the stats to construct the initial
111
0
    // FileManager entries).
112
0
    return err;
113
0
  }
114
0
115
0
  // Cache file 'stat' results and directories with absolutely paths.
116
0
  if (!Status.isDirectory() || llvm::sys::path::is_absolute(Path))
117
0
    StatCalls[Path] = Status;
118
0
119
0
  return std::error_code();
120
0
}