Skip to content

Commit

Permalink
[clang][deps] NFC: Move dependency consumer into header file
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 committed Aug 10, 2022
1 parent f4c21ab commit d5fcf8a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
#include "clang/Tooling/JSONCompilationDatabase.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/StringMap.h"
#include <string>
#include <vector>

namespace clang {
namespace tooling {
Expand Down Expand Up @@ -101,6 +104,42 @@ class DependencyScanningTool {
DependencyScanningWorker Worker;
};

class FullDependencyConsumer : public DependencyConsumer {
public:
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen)
: AlreadySeen(AlreadySeen) {}

void handleDependencyOutputOpts(const DependencyOutputOptions &) override {}

void handleFileDependency(StringRef File) override {
Dependencies.push_back(std::string(File));
}

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

void handleModuleDependency(ModuleDeps MD) override {
ClangModuleDeps[MD.ID.ContextHash + MD.ID.ModuleName] = std::move(MD);
}

void handleContextHash(std::string Hash) override {
ContextHash = std::move(Hash);
}

FullDependenciesResult getFullDependencies(
const std::vector<std::string> &OriginalCommandLine) const;

private:
std::vector<std::string> Dependencies;
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
llvm::MapVector<std::string, ModuleDeps, llvm::StringMap<unsigned>>
ClangModuleDeps;
std::string ContextHash;
std::vector<std::string> OutputPaths;
const llvm::StringSet<> &AlreadySeen;
};

} // end namespace dependencies
} // end namespace tooling
} // end namespace clang
Expand Down
92 changes: 29 additions & 63 deletions clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,76 +114,42 @@ DependencyScanningTool::getFullDependencies(
const std::vector<std::string> &CommandLine, StringRef CWD,
const llvm::StringSet<> &AlreadySeen,
llvm::Optional<StringRef> ModuleName) {
class FullDependencyPrinterConsumer : public DependencyConsumer {
public:
FullDependencyPrinterConsumer(const llvm::StringSet<> &AlreadySeen)
: AlreadySeen(AlreadySeen) {}

void
handleDependencyOutputOpts(const DependencyOutputOptions &Opts) override {}

void handleFileDependency(StringRef File) override {
Dependencies.push_back(std::string(File));
}

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

void handleModuleDependency(ModuleDeps MD) override {
ClangModuleDeps[MD.ID.ContextHash + MD.ID.ModuleName] = std::move(MD);
}

void handleContextHash(std::string Hash) override {
ContextHash = std::move(Hash);
}

FullDependenciesResult getFullDependencies(
const std::vector<std::string> &OriginalCommandLine) const {
FullDependencies FD;

FD.OriginalCommandLine =
ArrayRef<std::string>(OriginalCommandLine).slice(1);
FullDependencyConsumer Consumer(AlreadySeen);
llvm::Error Result =
Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName);
if (Result)
return std::move(Result);
return Consumer.getFullDependencies(CommandLine);
}

FD.ID.ContextHash = std::move(ContextHash);
FullDependenciesResult FullDependencyConsumer::getFullDependencies(
const std::vector<std::string> &OriginalCommandLine) const {
FullDependencies FD;

FD.FileDeps.assign(Dependencies.begin(), Dependencies.end());
FD.OriginalCommandLine = ArrayRef<std::string>(OriginalCommandLine).slice(1);

for (auto &&M : ClangModuleDeps) {
auto &MD = M.second;
if (MD.ImportedByMainFile)
FD.ClangModuleDeps.push_back(MD.ID);
}
FD.ID.ContextHash = std::move(ContextHash);

FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);
FD.FileDeps.assign(Dependencies.begin(), Dependencies.end());

FullDependenciesResult FDR;
for (auto &&M : ClangModuleDeps) {
auto &MD = M.second;
if (MD.ImportedByMainFile)
FD.ClangModuleDeps.push_back(MD.ID);
}

for (auto &&M : ClangModuleDeps) {
// TODO: Avoid handleModuleDependency even being called for modules
// we've already seen.
if (AlreadySeen.count(M.first))
continue;
FDR.DiscoveredModules.push_back(std::move(M.second));
}
FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);

FDR.FullDeps = std::move(FD);
return FDR;
}
FullDependenciesResult FDR;

private:
std::vector<std::string> Dependencies;
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
llvm::MapVector<std::string, ModuleDeps, llvm::StringMap<unsigned>> ClangModuleDeps;
std::string ContextHash;
std::vector<std::string> OutputPaths;
const llvm::StringSet<> &AlreadySeen;
};
for (auto &&M : ClangModuleDeps) {
// TODO: Avoid handleModuleDependency even being called for modules
// we've already seen.
if (AlreadySeen.count(M.first))
continue;
FDR.DiscoveredModules.push_back(std::move(M.second));
}

FullDependencyPrinterConsumer Consumer(AlreadySeen);
llvm::Error Result =
Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName);
if (Result)
return std::move(Result);
return Consumer.getFullDependencies(CommandLine);
FDR.FullDeps = std::move(FD);
return FDR;
}

0 comments on commit d5fcf8a

Please sign in to comment.