[Tooling/DependencyScanning] Enable passing a `vfs::FileSystem` object to `DependencyScanningTool`

Also include a unit test to validate that the `vfs::FileSystem` object is properly used.

Differential Revision: https://reviews.llvm.org/D129912
This commit is contained in:
Argyrios Kyrtzidis 2022-07-15 17:31:49 -07:00
parent b3fd3a9ac3
commit fbbabd4ca0
5 changed files with 45 additions and 8 deletions

View File

@ -68,7 +68,9 @@ struct FullDependenciesResult {
class DependencyScanningTool {
public:
/// Construct a dependency scanning tool.
DependencyScanningTool(DependencyScanningService &Service);
DependencyScanningTool(DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
llvm::vfs::createPhysicalFileSystem());
/// Print out the dependency information into a string using the dependency
/// file format that is specified in the options (-MD is the default) and

View File

@ -52,7 +52,8 @@ public:
/// using the regular processing run.
class DependencyScanningWorker {
public:
DependencyScanningWorker(DependencyScanningService &Service);
DependencyScanningWorker(DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
/// Run the dependency scanning tool for a given clang driver command-line,
/// and report the discovered dependencies to the provided consumer. If \p

View File

@ -50,8 +50,9 @@ FullDependencies::getCommandLineWithoutModulePaths() const {
}
DependencyScanningTool::DependencyScanningTool(
DependencyScanningService &Service)
: Worker(Service) {}
DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: Worker(Service, std::move(FS)) {}
llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(
const std::vector<std::string> &CommandLine, StringRef CWD,

View File

@ -264,7 +264,8 @@ private:
} // end anonymous namespace
DependencyScanningWorker::DependencyScanningWorker(
DependencyScanningService &Service)
DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()) {
PCHContainerOps = std::make_shared<PCHContainerOperations>();
PCHContainerOps->registerReader(
@ -274,8 +275,8 @@ DependencyScanningWorker::DependencyScanningWorker(
PCHContainerOps->registerWriter(
std::make_unique<ObjectFilePCHContainerWriter>());
auto OverlayFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
llvm::vfs::createPhysicalFileSystem());
auto OverlayFS =
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(std::move(FS));
InMemoryFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
OverlayFS->pushOverlay(InMemoryFS);
RealFS = OverlayFS;

View File

@ -14,19 +14,21 @@
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <algorithm>
#include <string>
using namespace clang;
using namespace tooling;
using namespace dependencies;
namespace {
@ -203,3 +205,33 @@ TEST(DependencyScanner, ScanDepsReuseFilemanagerHasInclude) {
EXPECT_EQ(convert_to_slash(Deps[4]), "/root/header.h");
EXPECT_EQ(convert_to_slash(Deps[5]), "/root/symlink.h");
}
TEST(DependencyScanner, ScanDepsWithFS) {
std::vector<std::string> CommandLine = {"clang", "-c", "test.cpp",
"-o"
"test.cpp.o"};
StringRef CWD = "/root";
auto VFS = new llvm::vfs::InMemoryFileSystem();
VFS->setCurrentWorkingDirectory(CWD);
auto Sept = llvm::sys::path::get_separator();
std::string HeaderPath =
std::string(llvm::formatv("{0}root{0}header.h", Sept));
std::string TestPath = std::string(llvm::formatv("{0}root{0}test.cpp", Sept));
VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n"));
VFS->addFile(TestPath, 0,
llvm::MemoryBuffer::getMemBuffer("#include \"header.h\"\n"));
DependencyScanningService Service(ScanningMode::DependencyDirectivesScan,
ScanningOutputFormat::Make);
DependencyScanningTool ScanTool(Service, VFS);
std::string DepFile;
ASSERT_THAT_ERROR(
ScanTool.getDependencyFile(CommandLine, CWD).moveInto(DepFile),
llvm::Succeeded());
using llvm::sys::path::convert_to_slash;
EXPECT_EQ(convert_to_slash(DepFile),
"test.cpp.o: /root/test.cpp /root/header.h\n");
}