whisperity updated this revision to Diff 141206.
whisperity added a comment.

Simplify the patch.


https://reviews.llvm.org/D45094

Files:
  include/clang/Basic/VirtualFileSystem.h
  include/clang/Tooling/Tooling.h
  lib/Basic/VirtualFileSystem.cpp
  lib/Tooling/Tooling.cpp
  unittests/Tooling/ToolingTest.cpp

Index: unittests/Tooling/ToolingTest.cpp
===================================================================
--- unittests/Tooling/ToolingTest.cpp
+++ unittests/Tooling/ToolingTest.cpp
@@ -402,24 +402,39 @@
   EXPECT_FALSE(Found);
 }
 
-TEST(ClangToolTest, BaseVirtualFileSystemUsage) {
+TEST(ClangToolVFSTest, VirtualFileSystemUsage) {
   FixedCompilationDatabase Compilations("/", std::vector<std::string>());
-  llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
-      new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
   llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
       new vfs::InMemoryFileSystem);
-  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
 
   InMemoryFileSystem->addFile(
-      "a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main() {}"));
+      "/a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main() {}"));
 
-  ClangTool Tool(Compilations, std::vector<std::string>(1, "a.cpp"),
-                 std::make_shared<PCHContainerOperations>(), OverlayFileSystem);
+  ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cpp"),
+                 std::make_shared<PCHContainerOperations>(),
+                 InMemoryFileSystem);
   std::unique_ptr<FrontendActionFactory> Action(
       newFrontendActionFactory<SyntaxOnlyAction>());
   EXPECT_EQ(0, Tool.run(Action.get()));
 }
 
+TEST(ClangToolVFSTest, VFSDoesntContainEveryFile) {
+  FixedCompilationDatabase Compilations("/", std::vector<std::string>());
+  llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
+      new vfs::InMemoryFileSystem);
+
+  InMemoryFileSystem->addFile(
+      "/a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("#include <cstdio>\n"
+                                                    "int main() {}"));
+
+  ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cpp"),
+                 std::make_shared<PCHContainerOperations>(),
+                 InMemoryFileSystem);
+  std::unique_ptr<FrontendActionFactory> Action(
+      newFrontendActionFactory<SyntaxOnlyAction>());
+  EXPECT_NE(0, Tool.run(Action.get()));
+}
+
 // Check getClangStripDependencyFileAdjuster doesn't strip args after -MD/-MMD.
 TEST(ClangToolTest, StripDependencyFileAdjuster) {
   FixedCompilationDatabase Compilations("/", {"-MD", "-c", "-MMD", "-w"});
Index: lib/Tooling/Tooling.cpp
===================================================================
--- lib/Tooling/Tooling.cpp
+++ lib/Tooling/Tooling.cpp
@@ -358,10 +358,10 @@
 ClangTool::ClangTool(const CompilationDatabase &Compilations,
                      ArrayRef<std::string> SourcePaths,
                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
-                     IntrusiveRefCntPtr<vfs::FileSystem> BaseFS)
+                     IntrusiveRefCntPtr<vfs::FileSystem> FileSystem)
     : Compilations(Compilations), SourcePaths(SourcePaths),
       PCHContainerOps(std::move(PCHContainerOps)),
-      OverlayFileSystem(new vfs::OverlayFileSystem(std::move(BaseFS))),
+      OverlayFileSystem(new vfs::OverlayFileSystem(std::move(FileSystem))),
       InMemoryFileSystem(new vfs::InMemoryFileSystem),
       Files(new FileManager(FileSystemOptions(), OverlayFileSystem)) {
   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
Index: lib/Basic/VirtualFileSystem.cpp
===================================================================
--- lib/Basic/VirtualFileSystem.cpp
+++ lib/Basic/VirtualFileSystem.cpp
@@ -325,6 +325,7 @@
 }
 
 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
+  // FIXME: OverlayFS containing another one in its stack could be flattened.
   FSList.push_back(FS);
   // Synchronize added file systems by duplicating the working directory from
   // the first one in the list.
@@ -366,6 +367,14 @@
   return {};
 }
 
+IntrusiveRefCntPtr<OverlayFileSystem>
+vfs::createOverlayOnRealFilesystem(IntrusiveRefCntPtr<FileSystem> TopFS) {
+  IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS =
+    new OverlayFileSystem(getRealFileSystem());
+  OverlayFS->pushOverlay(TopFS);
+  return OverlayFS;
+}
+
 clang::vfs::detail::DirIterImpl::~DirIterImpl() = default;
 
 namespace {
Index: include/clang/Tooling/Tooling.h
===================================================================
--- include/clang/Tooling/Tooling.h
+++ include/clang/Tooling/Tooling.h
@@ -303,14 +303,16 @@
   /// \param SourcePaths The source files to run over. If a source files is
   ///        not found in Compilations, it is skipped.
   /// \param PCHContainerOps The PCHContainerOperations for loading and creating
-  /// clang modules.
-  /// \param BaseFS VFS used for all underlying file accesses when running the
-  /// tool.
+  ///        clang modules.
+  /// \param FileSystem The Virtual File System that is used to retrieve file
+  ///        contents by the tool. In most cases, this should be an overlay upon
+  ///        the real file system. (See \p vfs::createOverlayOnRealFilesystem
+  ///        for easily creating an overlay.)
   ClangTool(const CompilationDatabase &Compilations,
             ArrayRef<std::string> SourcePaths,
             std::shared_ptr<PCHContainerOperations> PCHContainerOps =
                 std::make_shared<PCHContainerOperations>(),
-            IntrusiveRefCntPtr<vfs::FileSystem> BaseFS =
+            IntrusiveRefCntPtr<vfs::FileSystem> FileSystem =
                 vfs::getRealFileSystem());
 
   ~ClangTool();
Index: include/clang/Basic/VirtualFileSystem.h
===================================================================
--- include/clang/Basic/VirtualFileSystem.h
+++ include/clang/Basic/VirtualFileSystem.h
@@ -309,6 +309,11 @@
   iterator overlays_end() { return FSList.rend(); }
 };
 
+/// \brief Creates a \p vfs::OverlayFileSystem which overlays the given file
+/// system above the 'real' file system, as seen by the operating system.
+IntrusiveRefCntPtr<OverlayFileSystem>
+createOverlayOnRealFilesystem(IntrusiveRefCntPtr<FileSystem> TopFS);
+
 namespace detail {
 
 class InMemoryDirectory;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to