Skip to content

Commit

Permalink
[clangd][NFC] Rename FSProvider and getFileSystem
Browse files Browse the repository at this point in the history
Summary:
Clangd uses FSProvider to get threadsafe views into file systems. This
patch changes naming to make that more explicit.

Depends on D81920

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D81998
  • Loading branch information
kadircet committed Jun 19, 2020
1 parent 2dc2e47 commit 0628705
Show file tree
Hide file tree
Showing 38 changed files with 204 additions and 208 deletions.
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/ClangdLSPServer.cpp
Expand Up @@ -1340,7 +1340,7 @@ void ClangdLSPServer::onSemanticTokensEdits(
}

ClangdLSPServer::ClangdLSPServer(
class Transport &Transp, const FileSystemProvider &FSProvider,
class Transport &Transp, const ThreadsafeFS &FSProvider,
const clangd::CodeCompleteOptions &CCOpts,
const clangd::RenameOptions &RenameOpts,
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/ClangdLSPServer.h
Expand Up @@ -41,7 +41,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
/// for compile_commands.json in all parent directories of each file.
/// If UseDirBasedCDB is false, compile commands are not read from disk.
// FIXME: Clean up signature around CDBs.
ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider,
ClangdLSPServer(Transport &Transp, const ThreadsafeFS &FSProvider,
const clangd::CodeCompleteOptions &CCOpts,
const clangd::RenameOptions &RenameOpts,
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
Expand Down Expand Up @@ -207,7 +207,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
notify("$/progress", Params);
}

const FileSystemProvider &FSProvider;
const ThreadsafeFS &FSProvider;
/// Options used for code completion
clangd::CodeCompleteOptions CCOpts;
/// Options used for rename.
Expand Down
17 changes: 9 additions & 8 deletions clang-tools-extra/clangd/ClangdServer.cpp
Expand Up @@ -27,6 +27,7 @@
#include "refactor/Tweak.h"
#include "support/Logger.h"
#include "support/Markup.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/CompilerInstance.h"
Expand Down Expand Up @@ -129,8 +130,8 @@ ClangdServer::Options::operator TUScheduler::Options() const {
}

ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
const FileSystemProvider &FSProvider,
const Options &Opts, Callbacks *Callbacks)
const ThreadsafeFS &FSProvider, const Options &Opts,
Callbacks *Callbacks)
: FSProvider(FSProvider),
DynamicIdx(Opts.BuildDynamicSymbolIndex
? new FileIndex(Opts.HeavyweightDynamicSymbolIndex)
Expand Down Expand Up @@ -183,8 +184,8 @@ void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents,
Opts.ClangTidyOpts = tidy::ClangTidyOptions::getDefaults();
// FIXME: call tidy options builder on the worker thread, it can do IO.
if (GetClangTidyOptions)
Opts.ClangTidyOpts = GetClangTidyOptions(
*FSProvider.getFileSystem(/*CWD=*/llvm::None), File);
Opts.ClangTidyOpts =
GetClangTidyOptions(*FSProvider.view(/*CWD=*/llvm::None), File);
Opts.SuggestMissingIncludes = SuggestMissingIncludes;

// Compile command is set asynchronously during update, as it can be slow.
Expand Down Expand Up @@ -318,9 +319,9 @@ ClangdServer::formatOnType(llvm::StringRef Code, PathRef File, Position Pos,
llvm::Expected<size_t> CursorPos = positionToOffset(Code, Pos);
if (!CursorPos)
return CursorPos.takeError();
auto Style = format::getStyle(
format::DefaultFormatStyle, File, format::DefaultFallbackStyle, Code,
FSProvider.getFileSystem(/*CWD=*/llvm::None).get());
auto Style = format::getStyle(format::DefaultFormatStyle, File,
format::DefaultFallbackStyle, Code,
FSProvider.view(/*CWD=*/llvm::None).get());
if (!Style)
return Style.takeError();

Expand Down Expand Up @@ -549,7 +550,7 @@ void ClangdServer::switchSourceHeader(
// 2) if 1) fails, we use the AST&Index approach, it is slower but supports
// different code layout.
if (auto CorrespondingFile = getCorrespondingHeaderOrSource(
std::string(Path), FSProvider.getFileSystem(llvm::None)))
std::string(Path), FSProvider.view(llvm::None)))
return CB(std::move(CorrespondingFile));
auto Action = [Path = Path.str(), CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {
Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clangd/ClangdServer.h
Expand Up @@ -23,8 +23,8 @@
#include "refactor/Rename.h"
#include "refactor/Tweak.h"
#include "support/Cancellation.h"
#include "support/FSProvider.h"
#include "support/Function.h"
#include "support/ThreadsafeFS.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/FunctionExtras.h"
Expand Down Expand Up @@ -172,7 +172,7 @@ class ClangdServer {
/// those arguments for subsequent reparses. However, ClangdServer will check
/// if compilation arguments changed on calls to forceReparse().
ClangdServer(const GlobalCompilationDatabase &CDB,
const FileSystemProvider &FSProvider, const Options &Opts,
const ThreadsafeFS &FSProvider, const Options &Opts,
Callbacks *Callbacks = nullptr);

/// Add a \p File to the list of tracked C++ files or update the contents if
Expand Down Expand Up @@ -330,7 +330,7 @@ class ClangdServer {
formatCode(llvm::StringRef Code, PathRef File,
ArrayRef<tooling::Range> Ranges);

const FileSystemProvider &FSProvider;
const ThreadsafeFS &FSProvider;

Path ResourceDir;
// The index used to look up symbols. This could be:
Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clangd/CodeComplete.cpp
Expand Up @@ -36,9 +36,9 @@
#include "index/Index.h"
#include "index/Symbol.h"
#include "index/SymbolOrigin.h"
#include "support/FSProvider.h"
#include "support/Logger.h"
#include "support/Threading.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
Expand Down Expand Up @@ -1113,7 +1113,7 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
// NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
// the remapped buffers do not get freed.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
Input.ParseInput.FSProvider->getFileSystem(
Input.ParseInput.FSProvider->view(
Input.ParseInput.CompileCommand.Directory);
if (Input.Preamble.StatCache)
VFS = Input.Preamble.StatCache->getConsumingFS(std::move(VFS));
Expand Down Expand Up @@ -1364,7 +1364,7 @@ class CodeCompleteFlow {
}

CodeCompleteResult runWithoutSema(llvm::StringRef Content, size_t Offset,
const FileSystemProvider &FSProvider) && {
const ThreadsafeFS &FSProvider) && {
trace::Span Tracer("CodeCompleteWithoutSema");
// Fill in fields normally set by runWithSema()
HeuristicPrefix = guessCompletionPrefix(Content, Offset);
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/Compiler.cpp
Expand Up @@ -48,7 +48,7 @@ buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
for (const auto &S : Inputs.CompileCommand.CommandLine)
ArgStrs.push_back(S.c_str());

auto VFS = Inputs.FSProvider->getFileSystem(Inputs.CompileCommand.Directory);
auto VFS = Inputs.FSProvider->view(Inputs.CompileCommand.Directory);
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/Compiler.h
Expand Up @@ -18,7 +18,7 @@
#include "../clang-tidy/ClangTidyOptions.h"
#include "GlobalCompilationDatabase.h"
#include "index/Index.h"
#include "support/FSProvider.h"
#include "support/ThreadsafeFS.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/PrecompiledPreamble.h"
#include "clang/Tooling/CompilationDatabase.h"
Expand Down Expand Up @@ -46,7 +46,7 @@ struct ParseOptions {
/// Information required to run clang, e.g. to parse AST or do code completion.
struct ParseInputs {
tooling::CompileCommand CompileCommand;
const FileSystemProvider *FSProvider;
const ThreadsafeFS *FSProvider;
std::string Contents;
// Version identifier for Contents, provided by the client and opaque to us.
std::string Version = "null";
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/ParsedAST.cpp
Expand Up @@ -249,7 +249,7 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
trace::Span Tracer("BuildAST");
SPAN_ATTACH(Tracer, "File", Filename);

auto VFS = Inputs.FSProvider->getFileSystem(Inputs.CompileCommand.Directory);
auto VFS = Inputs.FSProvider->view(Inputs.CompileCommand.Directory);
if (Preamble && Preamble->StatCache)
VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));

Expand Down
12 changes: 6 additions & 6 deletions clang-tools-extra/clangd/Preamble.cpp
Expand Up @@ -10,8 +10,8 @@
#include "Compiler.h"
#include "Headers.h"
#include "SourceCode.h"
#include "support/FSProvider.h"
#include "support/Logger.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Basic/Diagnostic.h"
Expand Down Expand Up @@ -234,12 +234,12 @@ scanPreamble(llvm::StringRef Contents,
// than vfs::FileSystem, that way we can just use ParseInputs without this
// hack.
auto GetFSProvider = [](llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
class VFSProvider : public FileSystemProvider {
class VFSProvider : public ThreadsafeFS {
public:
VFSProvider(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: VFS(std::move(FS)) {}
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
getFileSystem(llvm::NoneType) const override {
view(llvm::NoneType) const override {
return VFS;
}

Expand Down Expand Up @@ -358,7 +358,7 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
CI.getPreprocessorOpts().WriteCommentListToPCH = false;

CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback);
auto VFS = Inputs.FSProvider->getFileSystem(Inputs.CompileCommand.Directory);
auto VFS = Inputs.FSProvider->view(Inputs.CompileCommand.Directory);
llvm::SmallString<32> AbsFileName(FileName);
VFS->makeAbsolute(AbsFileName);
auto StatCache = std::make_unique<PreambleFileStatusCache>(AbsFileName);
Expand Down Expand Up @@ -395,7 +395,7 @@ bool isPreambleCompatible(const PreambleData &Preamble,
llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
auto Bounds =
ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0);
auto VFS = Inputs.FSProvider->getFileSystem(Inputs.CompileCommand.Directory);
auto VFS = Inputs.FSProvider->view(Inputs.CompileCommand.Directory);
return compileCommandsAreEqual(Inputs.CompileCommand,
Preamble.CompileCommand) &&
Preamble.Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds,
Expand Down Expand Up @@ -423,7 +423,7 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
SPAN_ATTACH(Tracer, "File", FileName);
assert(llvm::sys::path::is_absolute(FileName) && "relative FileName!");
auto VFS = Baseline.StatCache->getConsumingFS(
Modified.FSProvider->getFileSystem(/*CWD=*/llvm::None));
Modified.FSProvider->view(/*CWD=*/llvm::None));
// First scan preprocessor directives in Baseline and Modified. These will be
// used to figure out newly added directives in Modified. Scanning can fail,
// the code just bails out and creates an empty patch in such cases, as:
Expand Down
14 changes: 7 additions & 7 deletions clang-tools-extra/clangd/SourceCode.cpp
Expand Up @@ -12,8 +12,8 @@
#include "Protocol.h"
#include "refactor/Tweak.h"
#include "support/Context.h"
#include "support/FSProvider.h"
#include "support/Logger.h"
#include "support/Threading.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
Expand Down Expand Up @@ -575,12 +575,12 @@ llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) {
return digest(Content);
}

format::FormatStyle
getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content,
const FileSystemProvider &FSProvider) {
auto Style = format::getStyle(
format::DefaultFormatStyle, File, format::DefaultFallbackStyle, Content,
FSProvider.getFileSystem(/*CWD=*/llvm::None).get());
format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
llvm::StringRef Content,
const ThreadsafeFS &FSProvider) {
auto Style = format::getStyle(format::DefaultFormatStyle, File,
format::DefaultFallbackStyle, Content,
FSProvider.view(/*CWD=*/llvm::None).get());
if (!Style) {
log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", File,
Style.takeError());
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/SourceCode.h
Expand Up @@ -15,7 +15,7 @@

#include "Protocol.h"
#include "support/Context.h"
#include "support/FSProvider.h"
#include "support/ThreadsafeFS.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
Expand Down Expand Up @@ -168,7 +168,7 @@ llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
/// though the latter may have been overridden in main()!
format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
llvm::StringRef Content,
const FileSystemProvider &FSProvider);
const ThreadsafeFS &FSProvider);

/// Cleanup and format the given replacements.
llvm::Expected<tooling::Replacements>
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/fuzzer/clangd-fuzzer.cpp
Expand Up @@ -16,7 +16,7 @@
#include "ClangdServer.h"
#include "CodeComplete.h"
#include "refactor/Rename.h"
#include "support/FSProvider.h"
#include "support/ThreadsafeFS.h"
#include <cstdio>
#include <sstream>

Expand All @@ -31,7 +31,7 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
auto Transport = newJSONTransport(In, llvm::nulls(),
/*InMirror=*/nullptr, /*Pretty=*/false,
/*Style=*/JSONStreamStyle::Delimited);
RealFileSystemProvider FS;
RealThreadsafeFS FS;
CodeCompleteOptions CCOpts;
CCOpts.EnableSnippets = false;
ClangdServer::Options Opts;
Expand Down
8 changes: 4 additions & 4 deletions clang-tools-extra/clangd/index/Background.cpp
Expand Up @@ -22,10 +22,10 @@
#include "index/Serialization.h"
#include "index/SymbolCollector.h"
#include "support/Context.h"
#include "support/FSProvider.h"
#include "support/Logger.h"
#include "support/Path.h"
#include "support/Threading.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
Expand Down Expand Up @@ -90,7 +90,7 @@ bool shardIsStale(const LoadedShard &LS, llvm::vfs::FileSystem *FS) {
} // namespace

BackgroundIndex::BackgroundIndex(
Context BackgroundContext, const FileSystemProvider &FSProvider,
Context BackgroundContext, const ThreadsafeFS &FSProvider,
const GlobalCompilationDatabase &CDB,
BackgroundIndexStorage::Factory IndexStorageFactory, size_t ThreadPoolSize,
std::function<void(BackgroundQueue::Stats)> OnProgress)
Expand Down Expand Up @@ -244,7 +244,7 @@ llvm::Error BackgroundIndex::index(tooling::CompileCommand Cmd) {
SPAN_ATTACH(Tracer, "file", Cmd.Filename);
auto AbsolutePath = getAbsolutePath(Cmd);

auto FS = FSProvider.getFileSystem(Cmd.Directory);
auto FS = FSProvider.view(Cmd.Directory);
auto Buf = FS->getBufferForFile(AbsolutePath);
if (!Buf)
return llvm::errorCodeToError(Buf.getError());
Expand Down Expand Up @@ -381,7 +381,7 @@ BackgroundIndex::loadProject(std::vector<std::string> MainFiles) {
Rebuilder.loadedShard(LoadedShards);
Rebuilder.doneLoading();

auto FS = FSProvider.getFileSystem(/*CWD=*/llvm::None);
auto FS = FSProvider.view(/*CWD=*/llvm::None);
llvm::DenseSet<PathRef> TUsToIndex;
// We'll accept data from stale shards, but ensure the files get reindexed
// soon.
Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clangd/index/Background.h
Expand Up @@ -16,9 +16,9 @@
#include "index/Index.h"
#include "index/Serialization.h"
#include "support/Context.h"
#include "support/FSProvider.h"
#include "support/Path.h"
#include "support/Threading.h"
#include "support/ThreadsafeFS.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Threading.h"
Expand Down Expand Up @@ -132,7 +132,7 @@ class BackgroundIndex : public SwapIndex {
/// rebuilt periodically (one per \p BuildIndexPeriodMs); otherwise, index is
/// rebuilt for each indexed file.
BackgroundIndex(
Context BackgroundContext, const FileSystemProvider &,
Context BackgroundContext, const ThreadsafeFS &,
const GlobalCompilationDatabase &CDB,
BackgroundIndexStorage::Factory IndexStorageFactory,
size_t ThreadPoolSize = 0, // 0 = use all hardware threads
Expand Down Expand Up @@ -178,7 +178,7 @@ class BackgroundIndex : public SwapIndex {
bool HadErrors);

// configuration
const FileSystemProvider &FSProvider;
const ThreadsafeFS &FSProvider;
const GlobalCompilationDatabase &CDB;
Context BackgroundContext;

Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/support/CMakeLists.txt
Expand Up @@ -19,11 +19,11 @@ include_directories(..)
add_clang_library(clangdSupport
Cancellation.cpp
Context.cpp
FSProvider.cpp
Logger.cpp
Markup.cpp
Shutdown.cpp
Threading.cpp
ThreadsafeFS.cpp
Trace.cpp

LINK_LIBS
Expand Down

0 comments on commit 0628705

Please sign in to comment.