#include "FS.h"
#include "clang/Basic/LLVM.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <optional>
#include <utility>
namespace clang {
namespace clangd {
PreambleFileStatusCache::PreambleFileStatusCache(llvm::StringRef MainFilePath){
assert(llvm::sys::path::is_absolute(MainFilePath));
llvm::SmallString<256> MainFileCanonical(MainFilePath);
llvm::sys::path::remove_dots(MainFileCanonical, true);
this->MainFilePath = std::string(MainFileCanonical);
}
void PreambleFileStatusCache::update(const llvm::vfs::FileSystem &FS,
llvm::vfs::Status S,
llvm::StringRef File) {
llvm::SmallString<32> PathStore(File);
if (FS.makeAbsolute(PathStore))
return;
llvm::sys::path::remove_dots(PathStore, true);
if (PathStore == MainFilePath)
return;
StatCache.insert({PathStore, std::move(S)});
}
std::optional<llvm::vfs::Status>
PreambleFileStatusCache::lookup(llvm::StringRef File) const {
llvm::SmallString<256> PathLookup(File);
llvm::sys::path::remove_dots(PathLookup, true);
auto I = StatCache.find(PathLookup);
if (I != StatCache.end())
return llvm::vfs::Status::copyWithNewName(I->getValue(), File);
return std::nullopt;
}
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
PreambleFileStatusCache::getProducingFS(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
class CollectFS : public llvm::vfs::ProxyFileSystem {
public:
CollectFS(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
PreambleFileStatusCache &StatCache)
: ProxyFileSystem(std::move(FS)), StatCache(StatCache) {}
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
openFileForRead(const llvm::Twine &Path) override {
auto File = getUnderlyingFS().openFileForRead(Path);
if (!File || !*File)
return File;
if (auto S = File->get()->status())
StatCache.update(getUnderlyingFS(), std::move(*S), Path.str());
return File;
}
llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override {
auto S = getUnderlyingFS().status(Path);
if (S)
StatCache.update(getUnderlyingFS(), *S, Path.str());
return S;
}
private:
PreambleFileStatusCache &StatCache;
};
return llvm::IntrusiveRefCntPtr<CollectFS>(
new CollectFS(std::move(FS), *this));
}
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
PreambleFileStatusCache::getConsumingFS(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) const {
class CacheVFS : public llvm::vfs::ProxyFileSystem {
public:
CacheVFS(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const PreambleFileStatusCache &StatCache)
: ProxyFileSystem(std::move(FS)), StatCache(StatCache) {}
llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override {
if (auto S = StatCache.lookup(Path.str()))
return *S;
return getUnderlyingFS().status(Path);
}
private:
const PreambleFileStatusCache &StatCache;
};
return llvm::IntrusiveRefCntPtr<CacheVFS>(new CacheVFS(std::move(FS), *this));
}
Path removeDots(PathRef File) {
llvm::SmallString<128> CanonPath(File);
llvm::sys::path::remove_dots(CanonPath, true);
return CanonPath.str().str();
}
}
}