Skip to content

Cherry-picks from upstream llvm.org related to clang dependency scanning #5090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DependencyConsumer {
/// 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
Expand Down
15 changes: 6 additions & 9 deletions clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "llvm/CAS/CASDB.h"
#include "llvm/CAS/CachingOnDiskFileSystem.h"

namespace clang {
namespace tooling {
namespace dependencies {
using namespace clang;
using namespace tooling;
using namespace dependencies;

std::vector<std::string> FullDependencies::getCommandLine(
llvm::function_ref<std::string(const ModuleID &, ModuleOutputKind)>
Expand Down Expand Up @@ -52,8 +52,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,
Expand Down Expand Up @@ -295,7 +296,3 @@ DependencyScanningTool::getFullDependencies(

return Consumer.getFullDependencies(CommandLine, FS);
}

} // end namespace dependencies
} // end namespace tooling
} // end namespace clang
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ class DependencyScanningAction : public tooling::ToolAction {
} // end anonymous namespace

DependencyScanningWorker::DependencyScanningWorker(
DependencyScanningService &Service)
DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()),
CASOpts(Service.getCASOpts()), UseCAS(Service.useCASScanning()) {
PCHContainerOps = std::make_shared<PCHContainerOperations>();
Expand All @@ -322,8 +323,8 @@ DependencyScanningWorker::DependencyScanningWorker(
std::make_unique<ObjectFilePCHContainerWriter>());

if (!Service.useCASScanning()) {
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;
Expand Down
3 changes: 2 additions & 1 deletion clang/tools/libclang/CDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void clang_experimental_FileDependencies_dispose(CXFileDependencies *ID) {

CXDependencyScannerWorker clang_experimental_DependencyScannerWorker_create_v0(
CXDependencyScannerService Service) {
return wrap(new DependencyScanningWorker(*unwrap(Service)));
return wrap(new DependencyScanningWorker(
*unwrap(Service), llvm::vfs::createPhysicalFileSystem()));
}

void clang_experimental_DependencyScannerWorker_dispose_v0(
Expand Down
44 changes: 39 additions & 5 deletions clang/unittests/Tooling/DependencyScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

namespace clang {
namespace tooling {
using namespace clang;
using namespace tooling;
using namespace dependencies;

namespace {

Expand Down Expand Up @@ -204,5 +206,37 @@ TEST(DependencyScanner, ScanDepsReuseFilemanagerHasInclude) {
EXPECT_EQ(convert_to_slash(Deps[5]), "/root/symlink.h");
}

} // end namespace tooling
} // end namespace clang
TEST(DependencyScanner, ScanDepsWithFS) {
std::vector<std::string> CommandLine = {"clang",
"-target",
"x86_64-apple-macosx10.7",
"-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, CASOptions(),
nullptr);
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");
}