Clang Project

clang_source_code/include/clang/Basic/FileSystemStatCache.h
1//===- FileSystemStatCache.h - Caching for 'stat' calls ---------*- C++ -*-===//
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/// \file
10/// Defines the FileSystemStatCache interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
15#define LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
16
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Allocator.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/VirtualFileSystem.h"
23#include <cstdint>
24#include <ctime>
25#include <memory>
26#include <string>
27#include <utility>
28
29namespace clang {
30
31/// Abstract interface for introducing a FileManager cache for 'stat'
32/// system calls, which is used by precompiled and pretokenized headers to
33/// improve performance.
34class FileSystemStatCache {
35  virtual void anchor();
36
37public:
38  virtual ~FileSystemStatCache() = default;
39
40  enum LookupResult {
41    /// We know the file exists and its cached stat data.
42    CacheExists,
43
44    /// We know that the file doesn't exist.
45    CacheMissing
46  };
47
48  /// Get the 'stat' information for the specified path, using the cache
49  /// to accelerate it if possible.
50  ///
51  /// \returns \c true if the path does not exist or \c false if it exists.
52  ///
53  /// If isFile is true, then this lookup should only return success for files
54  /// (not directories).  If it is false this lookup should only return
55  /// success for directories (not files).  On a successful file lookup, the
56  /// implementation can optionally fill in \p F with a valid \p File object and
57  /// the client guarantees that it will close it.
58  static bool get(StringRef Path, llvm::vfs::Status &Statusbool isFile,
59                  std::unique_ptr<llvm::vfs::File> *F,
60                  FileSystemStatCache *Cache, llvm::vfs::FileSystem &FS);
61
62protected:
63  // FIXME: The pointer here is a non-owning/optional reference to the
64  // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
65  // Optional needs some work to support references so this isn't possible yet.
66  virtual LookupResult getStat(StringRef Path, llvm::vfs::Status &Status,
67                               bool isFile,
68                               std::unique_ptr<llvm::vfs::File> *F,
69                               llvm::vfs::FileSystem &FS) = 0;
70};
71
72/// A stat "cache" that can be used by FileManager to keep
73/// track of the results of stat() calls that occur throughout the
74/// execution of the front end.
75class MemorizeStatCalls : public FileSystemStatCache {
76public:
77  /// The set of stat() calls that have been seen.
78  llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls;
79
80  using iterator =
81      llvm::StringMap<llvm::vfs::Status,
82                      llvm::BumpPtrAllocator>::const_iterator;
83
84  iterator begin() const { return StatCalls.begin(); }
85  iterator end() const { return StatCalls.end(); }
86
87  LookupResult getStat(StringRef Path, llvm::vfs::Status &Statusbool isFile,
88                       std::unique_ptr<llvm::vfs::File> *F,
89                       llvm::vfs::FileSystem &FS) override;
90};
91
92// namespace clang
93
94#endif // LLVM_CLANG_BASIC_FILESYSTEMSTATCACHE_H
95
clang::FileSystemStatCache::anchor
clang::FileSystemStatCache::LookupResult
clang::FileSystemStatCache::get
clang::FileSystemStatCache::getStat
clang::MemorizeStatCalls::StatCalls
clang::MemorizeStatCalls::begin
clang::MemorizeStatCalls::end
clang::MemorizeStatCalls::getStat