[clangd] Add a filename parameter to FileSystemProvider.

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: klimek, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D34151

llvm-svn: 305376
This commit is contained in:
Ilya Biryukov 2017-06-14 09:46:44 +00:00
parent ad270ec314
commit af0c04b30b
3 changed files with 24 additions and 7 deletions

View File

@ -59,7 +59,7 @@ Position clangd::offsetToPosition(StringRef Code, size_t Offset) {
}
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
RealFileSystemProvider::getTaggedFileSystem() {
RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
return make_tagged(vfs::getRealFileSystem(), VFSTag());
}
@ -156,7 +156,7 @@ void ClangdServer::addDocument(PathRef File, StringRef Contents) {
assert(FileContents.Draft &&
"No contents inside a file that was scheduled for reparse");
auto TaggedFS = FSProvider.getTaggedFileSystem();
auto TaggedFS = FSProvider.getTaggedFileSystem(FileStr);
Units.runOnUnit(
FileStr, *FileContents.Draft, CDB, PCHs, TaggedFS.Value,
[&](ClangdUnit const &Unit) {
@ -197,7 +197,7 @@ ClangdServer::codeComplete(PathRef File, Position Pos,
}
std::vector<CompletionItem> Result;
auto TaggedFS = FSProvider.getTaggedFileSystem();
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
// It would be nice to use runOnUnitWithoutReparse here, but we can't
// guarantee the correctness of code completion cache here if we don't do the
// reparse.

View File

@ -82,16 +82,21 @@ public:
class FileSystemProvider {
public:
virtual ~FileSystemProvider() = default;
/// Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.
/// Name of the file that will be parsed is passed in \p File.
///
/// \return A filesystem that will be used for all file accesses in clangd.
/// A Tag returned by this method will be propagated to all results of clangd
/// that will use this filesystem.
virtual Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> getTaggedFileSystem() = 0;
virtual Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
getTaggedFileSystem(PathRef File) = 0;
};
class RealFileSystemProvider : public FileSystemProvider {
public:
/// \return getRealFileSystem() tagged with default tag, i.e. VFSTag()
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> getTaggedFileSystem() override;
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
getTaggedFileSystem(PathRef File) override;
};
class ClangdServer;

View File

@ -172,9 +172,13 @@ public:
class MockFSProvider : public FileSystemProvider {
public:
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> getTaggedFileSystem() override {
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
getTaggedFileSystem(PathRef File) override {
IntrusiveRefCntPtr<vfs::InMemoryFileSystem> MemFS(
new vfs::InMemoryFileSystem);
if (ExpectedFile)
EXPECT_EQ(*ExpectedFile, File);
for (auto &FileAndContents : Files)
MemFS->addFile(FileAndContents.first(), time_t(),
llvm::MemoryBuffer::getMemBuffer(FileAndContents.second,
@ -186,6 +190,7 @@ public:
return make_tagged(OverlayFS, Tag);
}
llvm::Optional<SmallString<32>> ExpectedFile;
llvm::StringMap<std::string> Files;
VFSTag Tag = VFSTag();
};
@ -248,7 +253,10 @@ protected:
FileWithContents.second;
auto SourceFilename = getVirtualTestFilePath(SourceFileRelPath);
FS.ExpectedFile = SourceFilename;
Server.addDocument(SourceFilename, SourceContents);
auto Result = dumpASTWithoutMemoryLocs(Server, SourceFilename);
EXPECT_EQ(ExpectErrors, DiagConsumer.hadErrorInLastDiags());
return Result;
@ -307,6 +315,7 @@ int b = a;
FS.Files[FooH] = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
Server.addDocument(FooCpp, SourceContents);
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
@ -342,6 +351,7 @@ int b = a;
FS.Files[FooH] = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
Server.addDocument(FooCpp, SourceContents);
auto DumpParse1 = dumpASTWithoutMemoryLocs(Server, FooCpp);
@ -371,8 +381,9 @@ TEST_F(ClangdVFSTest, CheckVersions) {
auto FooCpp = getVirtualTestFilePath("foo.cpp");
const auto SourceContents = "int a;";
FS.Files[FooCpp] = SourceContents;
FS.Tag = "123";
FS.ExpectedFile = FooCpp;
FS.Tag = "123";
Server.addDocument(FooCpp, SourceContents);
EXPECT_EQ(DiagConsumer.lastVFSTag(), FS.Tag);
EXPECT_EQ(Server.codeComplete(FooCpp, Position{0, 0}).Tag, FS.Tag);
@ -419,6 +430,7 @@ int b = ;
// miss).
Position CompletePos = {2, 8};
FS.Files[FooCpp] = SourceContents;
FS.ExpectedFile = FooCpp;
Server.addDocument(FooCpp, SourceContents);