Skip to content
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
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/ScanningProjectModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ModuleDependencyScanner::scan(PathRef FilePath,
if (Mangler)
Mangler(Cmd, FilePath);

using namespace clang::tooling::dependencies;
using namespace clang::tooling;

llvm::SmallString<128> FilePathDir(FilePath);
llvm::sys::path::remove_filename(FilePathDir);
Expand Down
56 changes: 24 additions & 32 deletions clang/include/clang/DependencyScanning/DependencyScanningUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace clang {
namespace dependencies {

/// Graph of modular dependencies.
using ModuleDepsGraph = std::vector<clang::dependencies::ModuleDeps>;
using ModuleDepsGraph = std::vector<ModuleDeps>;

/// The full dependencies and module graph for a specific input.
struct TranslationUnitDeps {
Expand All @@ -31,22 +31,22 @@ struct TranslationUnitDeps {
/// The identifier of the C++20 module this translation unit exports.
///
/// If the translation unit is not a module then \c ID.ModuleName is empty.
clang::dependencies::ModuleID ID;
ModuleID ID;

/// A collection of absolute paths to files that this translation unit
/// directly depends on, not including transitive dependencies.
std::vector<std::string> FileDeps;

/// A collection of prebuilt modules this translation unit directly depends
/// on, not including transitive dependencies.
std::vector<clang::dependencies::PrebuiltModuleDep> PrebuiltModuleDeps;
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;

/// A list of modules this translation unit directly depends on, not including
/// transitive dependencies.
///
/// This may include modules with a different context hash when it can be
/// determined that the differences are benign for this compilation.
std::vector<clang::dependencies::ModuleID> ClangModuleDeps;
std::vector<ModuleID> ClangModuleDeps;

/// A list of module names that are visible to this translation unit. This
/// includes both direct and transitive module dependencies.
Expand All @@ -61,19 +61,18 @@ struct TranslationUnitDeps {
/// FIXME: If we add support for multi-arch builds in clang-scan-deps, we
/// should make the dependencies between commands explicit to enable parallel
/// builds of each architecture.
std::vector<clang::dependencies::Command> Commands;
std::vector<Command> Commands;

/// Deprecated driver command-line. This will be removed in a future version.
std::vector<std::string> DriverCommandLine;
};

class FullDependencyConsumer : public clang::dependencies::DependencyConsumer {
class FullDependencyConsumer : public DependencyConsumer {
public:
FullDependencyConsumer(
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen)
FullDependencyConsumer(const llvm::DenseSet<ModuleID> &AlreadySeen)
: AlreadySeen(AlreadySeen) {}

void handleBuildCommand(clang::dependencies::Command Cmd) override {
void handleBuildCommand(Command Cmd) override {
Commands.push_back(std::move(Cmd));
}

Expand All @@ -83,16 +82,15 @@ class FullDependencyConsumer : public clang::dependencies::DependencyConsumer {
Dependencies.push_back(std::string(File));
}

void handlePrebuiltModuleDependency(
clang::dependencies::PrebuiltModuleDep PMD) override {
void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {
PrebuiltModuleDeps.emplace_back(std::move(PMD));
}

void handleModuleDependency(clang::dependencies::ModuleDeps MD) override {
void handleModuleDependency(ModuleDeps MD) override {
ClangModuleDeps[MD.ID] = std::move(MD);
}

void handleDirectModuleDependency(clang::dependencies::ModuleID ID) override {
void handleDirectModuleDependency(ModuleID ID) override {
DirectModuleDeps.push_back(ID);
}

Expand All @@ -105,8 +103,8 @@ class FullDependencyConsumer : public clang::dependencies::DependencyConsumer {
}

void handleProvidedAndRequiredStdCXXModules(
std::optional<clang::dependencies::P1689ModuleInfo> Provided,
std::vector<clang::dependencies::P1689ModuleInfo> Requires) override {
std::optional<P1689ModuleInfo> Provided,
std::vector<P1689ModuleInfo> Requires) override {
ModuleName = Provided ? Provided->ModuleName : "";
llvm::transform(Requires, std::back_inserter(NamedModuleDeps),
[](const auto &Module) { return Module.ModuleName; });
Expand All @@ -116,34 +114,29 @@ class FullDependencyConsumer : public clang::dependencies::DependencyConsumer {

private:
std::vector<std::string> Dependencies;
std::vector<clang::dependencies::PrebuiltModuleDep> PrebuiltModuleDeps;
llvm::MapVector<clang::dependencies::ModuleID,
clang::dependencies::ModuleDeps>
ClangModuleDeps;
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
llvm::MapVector<ModuleID, ModuleDeps> ClangModuleDeps;
std::string ModuleName;
std::vector<std::string> NamedModuleDeps;
std::vector<clang::dependencies::ModuleID> DirectModuleDeps;
std::vector<ModuleID> DirectModuleDeps;
std::vector<std::string> VisibleModules;
std::vector<clang::dependencies::Command> Commands;
std::vector<Command> Commands;
std::string ContextHash;
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen;
const llvm::DenseSet<ModuleID> &AlreadySeen;
};

/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
using LookupModuleOutputCallback =
llvm::function_ref<std::string(const clang::dependencies::ModuleDeps &,
clang::dependencies::ModuleOutputKind)>;
llvm::function_ref<std::string(const ModuleDeps &, ModuleOutputKind)>;

/// A simple dependency action controller that uses a callback. If no callback
/// is provided, it is assumed that looking up module outputs is unreachable.
class CallbackActionController
: public clang::dependencies::DependencyActionController {
class CallbackActionController : public DependencyActionController {
public:
virtual ~CallbackActionController();

static std::string
lookupUnreachableModuleOutput(const clang::dependencies::ModuleDeps &MD,
clang::dependencies::ModuleOutputKind Kind) {
static std::string lookupUnreachableModuleOutput(const ModuleDeps &MD,
ModuleOutputKind Kind) {
llvm::report_fatal_error("unexpected call to lookupModuleOutput");
};

Expand All @@ -154,9 +147,8 @@ class CallbackActionController
}
}

std::string
lookupModuleOutput(const clang::dependencies::ModuleDeps &MD,
clang::dependencies::ModuleOutputKind Kind) override {
std::string lookupModuleOutput(const ModuleDeps &MD,
ModuleOutputKind Kind) override {
return LookupModuleOutput(MD, Kind);
}

Expand Down
49 changes: 20 additions & 29 deletions clang/include/clang/Tooling/DependencyScanningTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@
#include "clang/DependencyScanning/DependencyScanningUtils.h"
#include "clang/DependencyScanning/DependencyScanningWorker.h"
#include "clang/DependencyScanning/ModuleDepCollector.h"
#include "clang/Tooling/JSONCompilationDatabase.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
#include <functional>
#include <optional>
#include <string>
#include <vector>

namespace clang {
namespace tooling {
namespace dependencies {

struct P1689Rule {
std::string PrimaryOutput;
std::optional<clang::dependencies::P1689ModuleInfo> Provides;
std::vector<clang::dependencies::P1689ModuleInfo> Requires;
std::optional<dependencies::P1689ModuleInfo> Provides;
std::vector<dependencies::P1689ModuleInfo> Requires;
};

/// The high-level implementation of the dependency discovery tool that runs on
Expand All @@ -40,10 +36,9 @@ class DependencyScanningTool {
///
/// @param Service The parent service. Must outlive the tool.
/// @param FS The filesystem for the tool to use. Defaults to the physical FS.
DependencyScanningTool(
clang::dependencies::DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
llvm::vfs::createPhysicalFileSystem());
DependencyScanningTool(dependencies::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 All @@ -66,12 +61,11 @@ class DependencyScanningTool {
/// \returns A \c StringError with the diagnostic output if clang errors
/// occurred, P1689 dependency format rules otherwise.
llvm::Expected<P1689Rule>
getP1689ModuleDependencyFile(const clang::tooling::CompileCommand &Command,
StringRef CWD, std::string &MakeformatOutput,
getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD,
std::string &MakeformatOutput,
std::string &MakeformatOutputPath);
llvm::Expected<P1689Rule>
getP1689ModuleDependencyFile(const clang::tooling::CompileCommand &Command,
StringRef CWD) {
getP1689ModuleDependencyFile(const CompileCommand &Command, StringRef CWD) {
std::string MakeformatOutput;
std::string MakeformatOutputPath;

Expand All @@ -96,11 +90,11 @@ class DependencyScanningTool {
///
/// \returns a \c StringError with the diagnostic output if clang errors
/// occurred, \c TranslationUnitDeps otherwise.
llvm::Expected<clang::dependencies::TranslationUnitDeps>
llvm::Expected<dependencies::TranslationUnitDeps>
getTranslationUnitDependencies(
const std::vector<std::string> &CommandLine, StringRef CWD,
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen,
clang::dependencies::LookupModuleOutputCallback LookupModuleOutput,
const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput,
std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);

/// Given a compilation context specified via the Clang driver command-line,
Expand All @@ -109,12 +103,10 @@ class DependencyScanningTool {
/// TODO: this method should be removed as soon as Swift and our C-APIs adopt
/// CompilerInstanceWithContext. We are keeping it here so that it is easier
/// to coordinate with Swift and C-API changes.
llvm::Expected<clang::dependencies::TranslationUnitDeps>
getModuleDependencies(
llvm::Expected<dependencies::TranslationUnitDeps> getModuleDependencies(
StringRef ModuleName, const std::vector<std::string> &CommandLine,
StringRef CWD,
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen,
clang::dependencies::LookupModuleOutputCallback LookupModuleOutput);
StringRef CWD, const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput);

/// The following three methods provide a new interface to perform
/// by name dependency scan. The new interface's intention is to improve
Expand Down Expand Up @@ -144,11 +136,11 @@ class DependencyScanningTool {
/// arguments for dependencies.
/// @return An instance of \c TranslationUnitDeps if the scan is successful.
/// Otherwise it returns an error.
llvm::Expected<clang::dependencies::TranslationUnitDeps>
llvm::Expected<dependencies::TranslationUnitDeps>
computeDependenciesByNameWithContext(
StringRef ModuleName,
const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen,
clang::dependencies::LookupModuleOutputCallback LookupModuleOutput);
const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
dependencies::LookupModuleOutputCallback LookupModuleOutput);

/// @brief This method finializes the compiler instance. It finalizes the
/// diagnostics and deletes the compiler instance. Call this method
Expand All @@ -159,12 +151,11 @@ class DependencyScanningTool {
llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); }

private:
clang::dependencies::DependencyScanningWorker Worker;
std::unique_ptr<clang::dependencies::TextDiagnosticsPrinterWithOutput>
dependencies::DependencyScanningWorker Worker;
std::unique_ptr<dependencies::TextDiagnosticsPrinterWithOutput>
DiagPrinterWithOS;
};

} // end namespace dependencies
} // end namespace tooling
} // end namespace clang

Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Tooling/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

using namespace clang;
using namespace tooling;
using namespace clang::dependencies;
using namespace clang::tooling::dependencies;
using namespace dependencies;

DependencyScanningTool::DependencyScanningTool(
DependencyScanningService &Service,
Expand Down
3 changes: 1 addition & 2 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@

using namespace clang;
using namespace tooling;
using namespace clang::dependencies;
using namespace clang::tooling::dependencies;
using namespace dependencies;

namespace {

Expand Down
3 changes: 1 addition & 2 deletions clang/unittests/Tooling/DependencyScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

using namespace clang;
using namespace tooling;
using namespace clang::dependencies;
using namespace tooling::dependencies;
using namespace dependencies;

namespace {

Expand Down
Loading