r249385 - [VFS] Transition clang-format to use an in-memory FS.
Benjamin Kramer via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 6 03:04:10 PDT 2015
Author: d0k
Date: Tue Oct 6 05:04:08 2015
New Revision: 249385
URL: http://llvm.org/viewvc/llvm-project?rev=249385&view=rev
Log:
[VFS] Transition clang-format to use an in-memory FS.
Apart from being cleaner this also means that clang-format no longer has
access to the host file system. This isn't necessary because clang-format
never reads includes :)
Includes minor tweaks and bugfixes found in the VFS implementation while
running clang-format tests.
Modified:
cfe/trunk/include/clang/Basic/VirtualFileSystem.h
cfe/trunk/lib/Basic/VirtualFileSystem.cpp
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/tools/clang-format/ClangFormat.cpp
cfe/trunk/unittests/Basic/VirtualFileSystemTest.cpp
Modified: cfe/trunk/include/clang/Basic/VirtualFileSystem.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/VirtualFileSystem.h?rev=249385&r1=249384&r2=249385&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/VirtualFileSystem.h (original)
+++ cfe/trunk/include/clang/Basic/VirtualFileSystem.h Tue Oct 6 05:04:08 2015
@@ -274,8 +274,12 @@ class InMemoryFileSystem : public FileSy
public:
InMemoryFileSystem();
~InMemoryFileSystem() override;
+ /// Add a buffer to the VFS with a path. The VFS owns the buffer.
void addFile(const Twine &Path, time_t ModificationTime,
std::unique_ptr<llvm::MemoryBuffer> Buffer);
+ /// Add a buffer to the VFS with a path. The VFS does not own the buffer.
+ void addFileNoOwn(const Twine &Path, time_t ModificationTime,
+ llvm::MemoryBuffer *Buffer);
StringRef toString() const;
llvm::ErrorOr<Status> status(const Twine &Path) override;
Modified: cfe/trunk/lib/Basic/VirtualFileSystem.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/VirtualFileSystem.cpp?rev=249385&r1=249384&r2=249385&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/VirtualFileSystem.cpp (original)
+++ cfe/trunk/lib/Basic/VirtualFileSystem.cpp Tue Oct 6 05:04:08 2015
@@ -528,6 +528,13 @@ void InMemoryFileSystem::addFile(const T
}
}
+void InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
+ llvm::MemoryBuffer *Buffer) {
+ return addFile(P, ModificationTime,
+ llvm::MemoryBuffer::getMemBuffer(
+ Buffer->getBuffer(), Buffer->getBufferIdentifier()));
+}
+
static ErrorOr<detail::InMemoryNode *>
lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
const Twine &P) {
@@ -541,6 +548,15 @@ lookupInMemoryNode(const InMemoryFileSys
auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
while (true) {
+ // Skip over ".".
+ // FIXME: Also handle "..".
+ if (*I == ".") {
+ ++I;
+ if (I == E)
+ return Dir;
+ continue;
+ }
+
detail::InMemoryNode *Node = Dir->getChild(*I);
++I;
if (!Node)
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=249385&r1=249384&r2=249385&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Oct 6 05:04:08 2015
@@ -1792,18 +1792,17 @@ tooling::Replacements reformat(const For
if (Style.DisableFormat)
return tooling::Replacements();
- FileManager Files((FileSystemOptions()));
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new vfs::InMemoryFileSystem);
+ FileManager Files(FileSystemOptions(), InMemoryFileSystem);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager SourceMgr(Diagnostics, Files);
- std::unique_ptr<llvm::MemoryBuffer> Buf =
- llvm::MemoryBuffer::getMemBuffer(Code, FileName);
- const clang::FileEntry *Entry =
- Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
- SourceMgr.overrideFileContents(Entry, std::move(Buf));
- FileID ID =
- SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
+ InMemoryFileSystem->addFile(FileName, 0,
+ llvm::MemoryBuffer::getMemBuffer(Code, FileName));
+ FileID ID = SourceMgr.createFileID(Files.getFile(FileName), SourceLocation(),
+ clang::SrcMgr::C_User);
SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
std::vector<CharSourceRange> CharRanges;
for (const tooling::Range &Range : Ranges) {
Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=249385&r1=249384&r2=249385&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
+++ cfe/trunk/tools/clang-format/ClangFormat.cpp Tue Oct 6 05:04:08 2015
@@ -109,11 +109,11 @@ namespace clang {
namespace format {
static FileID createInMemoryFile(StringRef FileName, MemoryBuffer *Source,
- SourceManager &Sources, FileManager &Files) {
- const FileEntry *Entry = Files.getVirtualFile(FileName,
- Source->getBufferSize(), 0);
- Sources.overrideFileContents(Entry, Source, true);
- return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
+ SourceManager &Sources, FileManager &Files,
+ vfs::InMemoryFileSystem *MemFS) {
+ MemFS->addFileNoOwn(FileName, 0, Source);
+ return Sources.createFileID(Files.getFile(FileName), SourceLocation(),
+ SrcMgr::C_User);
}
// Parses <start line>:<end line> input to a pair of line numbers.
@@ -127,12 +127,15 @@ static bool parseLineRange(StringRef Inp
static bool fillRanges(MemoryBuffer *Code,
std::vector<tooling::Range> &Ranges) {
- FileManager Files((FileSystemOptions()));
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new vfs::InMemoryFileSystem);
+ FileManager Files(FileSystemOptions(), InMemoryFileSystem);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager Sources(Diagnostics, Files);
- FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files);
+ FileID ID = createInMemoryFile("<irrelevant>", Code, Sources, Files,
+ InMemoryFileSystem.get());
if (!LineRanges.empty()) {
if (!Offsets.empty() || !Lengths.empty()) {
llvm::errs() << "error: cannot use -lines with -offset/-length\n";
@@ -269,12 +272,15 @@ static bool format(StringRef FileName) {
outputReplacementsXML(Replaces);
llvm::outs() << "</replacements>\n";
} else {
- FileManager Files((FileSystemOptions()));
+ IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new vfs::InMemoryFileSystem);
+ FileManager Files(FileSystemOptions(), InMemoryFileSystem);
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceManager Sources(Diagnostics, Files);
- FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files);
+ FileID ID = createInMemoryFile(AssumedFileName, Code.get(), Sources, Files,
+ InMemoryFileSystem.get());
Rewriter Rewrite(Sources, LangOptions());
tooling::applyAllReplacements(Replaces, Rewrite);
if (Inplace) {
Modified: cfe/trunk/unittests/Basic/VirtualFileSystemTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Basic/VirtualFileSystemTest.cpp?rev=249385&r1=249384&r2=249385&view=diff
==============================================================================
--- cfe/trunk/unittests/Basic/VirtualFileSystemTest.cpp (original)
+++ cfe/trunk/unittests/Basic/VirtualFileSystemTest.cpp Tue Oct 6 05:04:08 2015
@@ -548,17 +548,29 @@ TEST_F(InMemoryFileSystemTest, OverlayFi
FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
auto Stat = FS.status("/");
ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
+ Stat = FS.status("/.");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
Stat = FS.status("/a");
ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
ASSERT_EQ("/a", Stat->getName());
}
+TEST_F(InMemoryFileSystemTest, OverlayFileNoOwn) {
+ auto Buf = MemoryBuffer::getMemBuffer("a");
+ FS.addFileNoOwn("/a", 0, Buf.get());
+ auto Stat = FS.status("/a");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
+ ASSERT_EQ("/a", Stat->getName());
+}
+
TEST_F(InMemoryFileSystemTest, OpenFileForRead) {
FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
auto File = FS.openFileForRead("/a");
ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
File = FS.openFileForRead("/a"); // Open again.
ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
+ File = FS.openFileForRead("/././a"); // Open again.
+ ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
File = FS.openFileForRead("/");
ASSERT_EQ(File.getError(), errc::invalid_argument) << FS.toString();
File = FS.openFileForRead("/b");
More information about the cfe-commits
mailing list