Skip to content

Commit 8a077cf

Browse files
committed
[clang][deps] Make the C++ API more type-safe
Scanner's C++ API accepts a set of modular dependencies the client has already seen and for which it doesn't need the full details. This is currently a set of strings, which somewhat implies that it should contain the set of module names. However, scanner internally expects the values to be in the format "{hash}{name}". Besides not being documented, this is very unintuitive. This patch makes this expectation explicit by changing the type to set of `ModuleID`. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D156492
1 parent 9016514 commit 8a077cf

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
1414
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
1515
#include "clang/Tooling/JSONCompilationDatabase.h"
16+
#include "llvm/ADT/DenseSet.h"
1617
#include "llvm/ADT/MapVector.h"
17-
#include "llvm/ADT/StringSet.h"
18-
#include "llvm/ADT/StringMap.h"
1918
#include <optional>
2019
#include <string>
2120
#include <vector>
@@ -125,25 +124,24 @@ class DependencyScanningTool {
125124
llvm::Expected<TranslationUnitDeps>
126125
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
127126
StringRef CWD,
128-
const llvm::StringSet<> &AlreadySeen,
127+
const llvm::DenseSet<ModuleID> &AlreadySeen,
129128
LookupModuleOutputCallback LookupModuleOutput);
130129

131130
/// Given a compilation context specified via the Clang driver command-line,
132131
/// gather modular dependencies of module with the given name, and return the
133132
/// information needed for explicit build.
134-
llvm::Expected<ModuleDepsGraph>
135-
getModuleDependencies(StringRef ModuleName,
136-
const std::vector<std::string> &CommandLine,
137-
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
138-
LookupModuleOutputCallback LookupModuleOutput);
133+
llvm::Expected<ModuleDepsGraph> getModuleDependencies(
134+
StringRef ModuleName, const std::vector<std::string> &CommandLine,
135+
StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
136+
LookupModuleOutputCallback LookupModuleOutput);
139137

140138
private:
141139
DependencyScanningWorker Worker;
142140
};
143141

144142
class FullDependencyConsumer : public DependencyConsumer {
145143
public:
146-
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen)
144+
FullDependencyConsumer(const llvm::DenseSet<ModuleID> &AlreadySeen)
147145
: AlreadySeen(AlreadySeen) {}
148146

149147
void handleBuildCommand(Command Cmd) override {
@@ -161,7 +159,7 @@ class FullDependencyConsumer : public DependencyConsumer {
161159
}
162160

163161
void handleModuleDependency(ModuleDeps MD) override {
164-
ClangModuleDeps[MD.ID.ContextHash + MD.ID.ModuleName] = std::move(MD);
162+
ClangModuleDeps[MD.ID] = std::move(MD);
165163
}
166164

167165
void handleContextHash(std::string Hash) override {
@@ -174,12 +172,11 @@ class FullDependencyConsumer : public DependencyConsumer {
174172
private:
175173
std::vector<std::string> Dependencies;
176174
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
177-
llvm::MapVector<std::string, ModuleDeps, llvm::StringMap<unsigned>>
178-
ClangModuleDeps;
175+
llvm::MapVector<ModuleID, ModuleDeps> ClangModuleDeps;
179176
std::vector<Command> Commands;
180177
std::string ContextHash;
181178
std::vector<std::string> OutputPaths;
182-
const llvm::StringSet<> &AlreadySeen;
179+
const llvm::DenseSet<ModuleID> &AlreadySeen;
183180
};
184181

185182
/// A simple dependency action controller that uses a callback. If no callback

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Lex/PPCallbacks.h"
1818
#include "clang/Serialization/ASTReader.h"
1919
#include "llvm/ADT/DenseMap.h"
20+
#include "llvm/ADT/Hashing.h"
2021
#include "llvm/ADT/StringSet.h"
2122
#include "llvm/Support/raw_ostream.h"
2223
#include <optional>
@@ -296,15 +297,17 @@ class ModuleDepCollector final : public DependencyCollector {
296297
} // end namespace clang
297298

298299
namespace llvm {
300+
inline hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID) {
301+
return hash_combine(ID.ModuleName, ID.ContextHash);
302+
}
303+
299304
template <> struct DenseMapInfo<clang::tooling::dependencies::ModuleID> {
300305
using ModuleID = clang::tooling::dependencies::ModuleID;
301306
static inline ModuleID getEmptyKey() { return ModuleID{"", ""}; }
302307
static inline ModuleID getTombstoneKey() {
303308
return ModuleID{"~", "~"}; // ~ is not a valid module name or context hash
304309
}
305-
static unsigned getHashValue(const ModuleID &ID) {
306-
return hash_combine(ID.ModuleName, ID.ContextHash);
307-
}
310+
static unsigned getHashValue(const ModuleID &ID) { return hash_value(ID); }
308311
static bool isEqual(const ModuleID &LHS, const ModuleID &RHS) {
309312
return LHS == RHS;
310313
}

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ llvm::Expected<P1689Rule> DependencyScanningTool::getP1689ModuleDependencyFile(
145145
llvm::Expected<TranslationUnitDeps>
146146
DependencyScanningTool::getTranslationUnitDependencies(
147147
const std::vector<std::string> &CommandLine, StringRef CWD,
148-
const llvm::StringSet<> &AlreadySeen,
148+
const llvm::DenseSet<ModuleID> &AlreadySeen,
149149
LookupModuleOutputCallback LookupModuleOutput) {
150150
FullDependencyConsumer Consumer(AlreadySeen);
151151
CallbackActionController Controller(LookupModuleOutput);
@@ -158,7 +158,7 @@ DependencyScanningTool::getTranslationUnitDependencies(
158158

159159
llvm::Expected<ModuleDepsGraph> DependencyScanningTool::getModuleDependencies(
160160
StringRef ModuleName, const std::vector<std::string> &CommandLine,
161-
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
161+
StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
162162
LookupModuleOutputCallback LookupModuleOutput) {
163163
FullDependencyConsumer Consumer(AlreadySeen);
164164
CallbackActionController Controller(LookupModuleOutput);

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,7 @@ class FullDeps {
472472
mutable size_t InputIndex;
473473

474474
bool operator==(const IndexedModuleID &Other) const {
475-
return std::tie(ID.ModuleName, ID.ContextHash) ==
476-
std::tie(Other.ID.ModuleName, Other.ID.ContextHash);
475+
return ID == Other.ID;
477476
}
478477

479478
bool operator<(const IndexedModuleID &Other) const {
@@ -493,7 +492,7 @@ class FullDeps {
493492

494493
struct Hasher {
495494
std::size_t operator()(const IndexedModuleID &IMID) const {
496-
return llvm::hash_combine(IMID.ID.ModuleName, IMID.ID.ContextHash);
495+
return llvm::hash_value(IMID.ID);
497496
}
498497
};
499498
};
@@ -880,7 +879,7 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
880879

881880
for (unsigned I = 0; I < Pool.getThreadCount(); ++I) {
882881
Pool.async([&, I]() {
883-
llvm::StringSet<> AlreadySeenModules;
882+
llvm::DenseSet<ModuleID> AlreadySeenModules;
884883
while (auto MaybeInputIndex = GetNextInputIndex()) {
885884
size_t LocalIndex = *MaybeInputIndex;
886885
const tooling::CompileCommand *Input = &Inputs[LocalIndex];

0 commit comments

Comments
 (0)